I'm trying to set the color of the selected row in a UITableView, using this code:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 开发者_开发百科
cell.backgroundColor = [UIColor redColor];
}
}
The problem is, it's setting the color on all the rows. Furthermore, when I do select a specific row, it highlights that row in blue. How do I get it to just highlight the selected row, in red?
The easiest way to me is to subclass UITableViewCell
and overwrite the setSelected:
method (leave everything else the same if you need to)
some sample code from an old project:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
UIView *bg = [[UIView alloc] initWithFrame:self.frame];
bg.backgroundColor = [UIColor colorWithRed:.916 green:.9648 blue:.9844 alpha:1.0];
self.selectedBackgroundView = bg;
[bg release];
// Configure the view for the selected state
}
精彩评论