UIView作为UITableViewCell中的分隔符和行选择的颜色

时间:2011-11-11 12:53:00

标签: iphone uitableview

我的应用程序中有一个TableView,我需要更改分隔符的高度和颜色。在SO中浏览帮助我找出解决方案。 所以我基本上在我的单元格中添加一个UIView并将其用作“假”分隔符:

UIView *colorSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, 53, cell.frame.size.width, 4)];
    colorSeparator.backgroundColor = [UIColor yellowColor];
    [cell.contentView addSubview:colorSeparator];
    [colorSeparator release];

但现在我注意到,当点击该行时,选择颜色适用于我的假分隔符。有谁知道怎么可以避免它?感谢您的时间:)

1 个答案:

答案 0 :(得分:2)

您可以在UITableViewCell的setSelected:animated:setHighlighted:animated:方法中恢复分隔符的颜色。

// just edited your function, it was missing a square bracket
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    UIColor *c = [[colorSeparator.backgroundColor retain] autorelease];
    [super setHighlighted:highlighted animated:animated];
    colorSeparator.backgroundColor = c;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    UIColor *c = [[colorSeparator.backgroundColor retain] autorelease];
    [super setSelected:selected animated:animated];
    colorSeparator.backgroundColor = c;
}