简体   繁体   中英

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. 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. Upon selecting a cell, the background of my cell changes to my customColor , but the background on the accessory remains gray. SOS!

The general consensus seems to that changing the color of the cell's backgroundView will affect the accessory

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. You need to give your cell a background view before you can set the color of that view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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