Changing backgroundcolor on custom UITableViewCell won't show
I'm trying to change the background color of a custom cell when clicked.. Here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath];
if (...condition..开发者_JS百科.) {
cell.backgroundColor = [UIColor clearColor];
} else {
cell.backgroundColor = [UIColor yellowColor];
}
}
Now, I did debug, and the cell.backgroundColor = [UIColor clearColor]; gets executed, but the cell stays yellow!!! This only happens to some of the cells (ones that are yellow, and reused)..
Any suggestions?
I got it to work. Had to use aCell.contentView.backgroundColor instead of aCell.backgroundColor
That did the trick for me.
The only way you can persist the changes is to save it somewhere and use it while setting up the cell in tableView:cellForRowAtIndexPath:
method. So, maintain a set of cells which have a yellow background and while setting up a cell, check whether it is in the set. If yes, assign it a yellow background and if no, set it to clear color.
Change the cell background color in custom cell class, where we have a method setbackground:.
精彩评论