30

Whenever I add an accessoryView to my UITableViewCell, it doesn't carry the background color across? I'm setting a UISwitch as my accessoryView, and the color I have set in the cell.backgroundColor property only effects the contentView and not the accessoryView. I have tried everything to set them to the same value. I tried to set the cell.backgroundView.backgroundColor and the cell.accessoryView.backgroundColor properties to the color I want but nothing is working. I also tried creating a subview inside contentView, which solved the backgroundColor problem (by avoiding it), but it creates the problem, where the switch sits on top of the cell.textLabel when the text is too long.

Is there are way I can modify the background color of the accessoryView without creating a subview in contentView, or to alter the length of the cell.textLabel without subclassing UITableViewCell?

dandan78
  • 13,328
  • 13
  • 64
  • 78
Zak
  • 12,213
  • 21
  • 59
  • 105

5 Answers5

69

Upon reading the documentation (a novel idea), I found the article, "A Closer Look at Table-View Cells". It helped me understand the composition of the cells, and I found my answer...

cells look like this...

alt text

Since the cell.accessoryView is a sister view to cell.contentView I had to ask the cell.contentView for its superview, and then I was able to change the background color for both views at once. Here's what the code looks like...

// Cell Formatting
cell.contentView.superview.backgroundColor = [UIColor greenColor];

I know it's really simple, but I'm a newbie and it took me ages to slow down and read the doc. Hopefully, this helps some other folks out there!

Zak
  • 12,213
  • 21
  • 59
  • 105
  • 7
    I know its a bit late, but you need to set the color of the cell in the `tableView:willDisplayCell:forRowAtIndexPath:` method :) – Illep Jan 26 '12 at 16:42
  • 2
    This helped me, but as an additional step I had to make sure not to call any `setSelected` methods on the `superclass` of the `UITableViewCell`. It looks like this sets the background color of the **accessoryView** to the default selection color. – Aerows Nov 10 '16 at 14:21
  • Thanks... Helps me a lot Save my time :) :) (y) – Anjali jariwala Sep 27 '18 at 10:13
  • That documentation is deceptive. The purple square marked accessory view is not where the accessory view frame is. It's probably smaller than that. So what you see there is not the accessory view background, but the cell's main view (around the accessory view). If you set the cell background, you will set the background for the cell content also. – Victor Engel Apr 28 '19 at 14:27
47
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor yellowColor];
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Aalok Bhatwal
  • 471
  • 1
  • 4
  • 3
  • But I think coloring stuff should belongs to the view class. Adarsh VC's solution is better because It could be placed in awakeFromNib. – johnlinvc Aug 16 '12 at 03:36
15
 UIView *myView = [[UIView alloc] initWithFrame:cell.frame];
 myView.backgroundColor = [UIColor colorWithRed:214.00/255.00 green:233.00/255.00 blue:247.00/255.00 alpha:1.0];
 cell.backgroundView = myView;
 [myView release];
Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
3

If you want to blend the accessory view background color with the background color of the cell, in iOS8 and Swift this worked like a charm in the tableView(_, cellForRowAtIndexPath:) method:

let color = cell.contentView.backgroundColor
cell.backgroundColor = color
jay492355
  • 594
  • 5
  • 12
1

@Zak, first of all thanks for bringing to my attention the details of the cell layout. Very helpful!
I would just like to point out that the following code:

self.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"cell_background.png"]];

worked for me. The result was a background image stretching over whole cell. AccessoryTypeView didn't cover it! Important part is to put this code into layoutSubviews method of your custom cell class and not into cellForRowAtIndexPath found in TableViewController class. In my case I defined a class CustomCell and inside of it I have defined labels, image views etc.

NOTE:The following code wasn't working:

cell.contentView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"cell_background.png"]];

when put inside tableView:(UITableView *)tableView cellForRowAtIndexPat method. It was giving me the background covering the cell but AccessoryTypeView was above it...

luigi7up
  • 5,779
  • 2
  • 48
  • 58