自定义的UITableViewCell背景颜色不正确

3
我有一个基于Storyboard原型单元格的自定义UITableViewCell。在我的自定义单元格类中,我根据单元格状态“selected”或“highlighted”对单元格进行着色,代码大致如下:
- (void)updateCellDisplay {
    if (self.selected || self.highlighted) {
        self.label.textColor = [UIColor whiteColor];
        self.backgroundColor = [UIColor myLightBlueColor];
    } else {
        self.label.textColor = [UIColor blackColor];
        self.backgroundColor = [UIColor whiteColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    [self updateCellDisplay];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    [self updateCellDisplay];
}

现在我遇到了一个奇怪的问题,就是在iOS7上,选中后单元格会完全变成蓝色,标签不再可见。看起来像是单元格被覆盖了前景色。在iOS7之前的所有iOS版本上都能正常工作。
可能是什么原因呢?

我发现这与自定义UITableViewCell的selectedBackgroundView有关。你看到的蓝色是通过以下方式应用的selectedBackgroundView: UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:drawerCell.frame]; selectedBackgroundView.backgroundColor = [UIColor customLightBlueColor]; drawerCell.selectedBackgroundView = selectedBackgroundView; 它覆盖了UITableViewCell内容视图中的所有其他视图。这只会在iOS7上发生。我认为这是一个错误。有人知道解决方法吗? - Stefan Arn
1个回答

0

看起来单元格的contentView在内容上方。我曾经遇到过类似的问题,并通过在cellForRowAtIndexPath中将自定义项添加到contentView中解决了它。

在我的情况下,这些项没有接收到触摸事件,因为contentView在上面。

[[cell contentView] addSubview:cell.itemTitleView]

希望能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接