选中后更改UITableViewCell背景颜色

时间:2010-10-28 12:47:40

标签: iphone

我有一个UItableview,我想在它被选中时更改单元格背景并保持它像那样我试图使用                 [cell setBackgroundColor:[UIColor purpleColor]]; 但这使得其他细胞同时变色,我不知道为什么 我也试过用   [cell.textLabel setBackgroundColor:[UIColor purpleColor]]; 但是当我选择另一个单元格时,背景会变回白色 所以解决这个问题的任何想法??

3 个答案:

答案 0 :(得分:3)

  

UIView *backgroundView = [[UIView alloc] initWithFrame:cell.selectedBackgroundView.frame];
[backgroundView setBackgroundColor:[UIColor purpleColor]];
[cell setSelectedBackgroundView:backgroundView];
[backgroundView release];

答案 1 :(得分:0)

从3.0开始,在willDisplayCell方法中设置它:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];

}

答案 2 :(得分:0)

自己创建UITableViewCell类,即MyTableViewCell并重新实现所选择的函数。

-(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];
}
相关问题