为UITableViewCell附件的背景设置自定义颜色

时间:2015-01-16 23:19:00

标签: ios objective-c uitableview

我试图让我的表格查看单元格在选中时显示自定义复选标记图像。问题是复选标记图像背后的颜色是灰色而不是我提供的自定义单元格颜色。

我之前已经问过这个问题了 - 我已经阅读并尝试了以下建议:

其中。普遍的共识似乎是改变细胞的backgroundView颜色会影响配件,但对我来说似乎并非如此。这是我目前的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryView = [[UIImageView alloc] initWithImage:_checkImage];

    [self setCellColor:_customColor forCell:cell];
}

- (void)setCellColor:(UIColor *)color forCell:(UITableViewCell *)cell
{
    cell.contentView.backgroundColor = color;
    cell.backgroundView.backgroundColor = color;
    cell.backgroundColor = color;
 }

请注意,我的checkImage具有透明背景。选择一个单元格后,我单元格的背景会变为我的customColor,但附件上的背景仍为灰色。 SOS!

1 个答案:

答案 0 :(得分:0)

  

普遍的共识似乎是改变单元格backgroundView的颜色会影响配件

这是正确的。问题是你没有这样做。你这么说:

cell.backgroundView.backgroundColor = color;

但是cell.backgroundView是零,所以这条线是毫无意义的。在设置该视图的颜色之前,您需要为您的单元格提供背景视图。

相关问题