2014-01-21 30 views
0

单击时我正在更改单元格颜色,并且还在更改单元格底部的小自定义分隔符。但是,当我拨打deselectRowAtIndexPath时,分隔符颜色会恢复到忽略我的更改之前的颜色。为什么会发生这种情况,我该如何完成这个简单的任务? 这里是我的代码:在UITableView中选择后,无法更改分隔符(自定义UIView)颜色

[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
UIView *separator = [cell viewWithTag:SEPARATOR]; 
separator.backgroundColor = _colorOrangeSelected; 

回答

1

我已经通过继承UITableViewCell我的细胞和压倒一切的setSelectedsetHighlighted解决问题方法。我猜想本机实现只是以一种奇怪的方式混淆了子视图和背景,忽略了它们的最新状态。

1

这里是一个解决方案:

  • 拖动UIImageView你的细胞,并使其分离。用两个不同的图像设置imageHightLightImage

当选中单元格时,您的自定义单元格将自动刷新子视图状态,但您必须准备两个图像。

1

UIView不能有突出显示的颜色。 您可以尝试以下解决方案: 1.使用的UIButton或UIImageView的可设置highlightedColor或highlightedImage 2.使用的drawRect:

- (void)drawRect:(CGRect)rect { 
    [super drawRect:rect]; 
    if (self.cellDevideLineColor != nil) { 
     CGContextRef context = UIGraphicsGetCurrentContext(); 

     [self.cellDevideLineColor set]; 
     CGContextSetLineWidth(context, kCellLineHeight); 

     CGContextMoveToPoint(context, 0, rect.size.height); 
     CGContextAddLineToPoint(context, self.width, rect.size.height); 
     CGContextStrokePath(context); 
    } 
} 
相关问题