简体   繁体   English

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

[英]Setting custom color for background of UITableViewCell accessory

I'm trying to get my table view cells to show a custom checkmark image when selected. 我正在尝试让我的表格视图单元格在选中时显示自定义选中标记图像。 The issue is that the color behind the checkmark image is gray instead of the custom cell color that I provide. 问题是,选中标记图像后面的颜色是灰色,而不是我提供的自定义单元格颜色。

I'm aware this question has been asked before - I've read through and attempted to the following suggestions: 我知道之前曾有人问过这个问题-我已通读并尝试了以下建议:

Among others. 其中。 The general consensus seems to that changing the color of the cell's backgroundView will affect the accessory, but this does not seem to be the case for me. 总体共识似乎是,更改单元格backgroundView的颜色会影响附件,但对我而言似乎并非如此。 Here is my current code: 这是我当前的代码:

- (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;
 }

Note that my checkImage has a transparent background. 请注意,我的checkImage具有透明背景。 Upon selecting a cell, the background of my cell changes to my customColor , but the background on the accessory remains gray. 选择单元格后,单元格的背景将更改为customColor ,但附件上的背景仍为灰色。 SOS! SOS!

The general consensus seems to that changing the color of the cell's backgroundView will affect the accessory 普遍的共识似乎是,更改单元格backgroundView的颜色会影响附件

That is correct. 那是对的。 The problem is that you are not doing that. 问题是您没有这样做。 You are saying this: 你是这样说的:

cell.backgroundView.backgroundColor = color;

But cell.backgroundView is nil, so that line is pointless. 但是cell.backgroundView为nil,所以该行毫无意义。 You need to give your cell a background view before you can set the color of that view. 您需要先单元格提供背景视图,然后才能设置该视图的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM