Reputation: 1275
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!
Upvotes: 1
Views: 1042
Reputation: 1482
Use
self.selectedBackgroundView?.backgroundColor = .red
to change the UITableViewCell
background color when selected and
self.contentView.backgroundColor = .green
to change the UITableViewCell
background color when unselected/default
Upvotes: 0
Reputation: 534958
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.
Upvotes: 1