开发者

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:

- (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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜