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

问题描述:

我的应用程序中有一个TableView,我需要更改高度和分隔符的颜色。在SO浏览帮助我找出解决方案。 所以我基本上都在我的小区追加一个UIView,并以此作为“假”分离器:作为UITableViewCell中的分隔符和行选择的颜色UIView

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

但现在我注意到,当行被窃听,选择的颜色适用于我的假隔膜。有谁知道如何避免它? Thx在您的时间建议:)

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

// 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; 
} 
+0

Thx很多!所以你的意思是我应该sublcass UITableViewCell并重写这两个方法? –

+0

是的,您应该继承UITableViewCell,覆盖此方法并从UITableView委托方法返回新类的对象。 –

+0

thx,它的工作原理!为您投票! –