change UITableViewCell background color when selected
i have a UItableview and i want to change the cell background when it is sel开发者_开发知识库eceted and keep it like that so i tried to use [cell setBackgroundColor:[UIColor purpleColor]]; but that made other cells get colored at same time and i dont know why i also tried to use [cell.textLabel setBackgroundColor:[UIColor purpleColor]]; but when i select another cell the background go back to white color so any ideas for solving that problem??
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.selectedBackgroundView.frame];
[backgroundView setBackgroundColor:[UIColor purpleColor]];
[cell setSelectedBackgroundView:backgroundView];
[backgroundView release];
since 3.0, set it in the willDisplayCell method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
Subclass UITableViewCell by yourself class, i.e. MyTableViewCell and reimplement the selected function.
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
UIView *backgroundView = [[UIView alloc] initWithFrame:self.selectedBackgroundView.frame];
[backgroundView setBackgroundColor:[UIColor purpleColor]];
[self setSelectedBackgroundView:backgroundView];
}
精彩评论