简体   繁体   English

iOS / iPhone-设置任意行的颜色

[英]iOS / iPhone - setting the color of an arbitrary row

I'm trying to set the color of the selected row in a UITableView, using this code: 我正在尝试使用以下代码在UITableView中设置所选行的颜色:

- (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) 对我来说,最简单的方法是将UITableViewCell子类化并覆盖setSelected:方法(如果需要,其他所有内容都保留相同)

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM