Why doesn't UITableViewCell background color work (set in interface builder)?

IosUitableviewCocoa TouchUikit

Ios Problem Overview


Why doesn't UITableViewCell background color work (set in interface builder)?

I note from some searching that the follow code set in your custom subclass of UITableViewController does work (see below):

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor redColor];
}

But I would still like to just understand why interface builder has a background color setting for the TableViewCell, which effectively doesn't seem to work?

Ios Solutions


Solution 1 - Ios

It's a bit late, but I just ran into this issue... Setting the background color on the contentView works fine:

cell.contentView.backgroundColor = [UIColor redColor];

Solution 2 - Ios

What worked for me is creating my own UIView object to be set as the background view of the UITableViewCell. In the cellForRowAtIndexPath:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];

Solution 3 - Ios

Trying to set the background colour of the UITableViewCell itself is not how you should be doing it. A table cell has a collection of five useful views you can access, one of which is backgroundView.

Recommended reading:

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

and then:

http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html

Solution 4 - Ios

You should always provide changes for cells in tableView:willDisplayCell:forRowAtIndexPath: instead of the tableView:cellForRowAtIndexPath: method, as per Apple Documentation.

This Works !!

Solution 5 - Ios

[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];
 

Solution 6 - Ios

Make sure before defining a cell color your backgroundView is null

cell.backgroundView = nil;
cell.backgroundColor = [UIColor redColor];

Solution 7 - Ios

This is simple and works: in interface builder, add a view object to the UITableViewCell of the same dimensions, and place your controls inside that view. Then you get whatever background color you want.

Edit: here's a reason to accept, it just struck me: take a careful look at what you get when you double click on a UITableViewCell in IB that you place into your nib, to start adding stuff to it: you don't get a plain rectangle looking view, you get an oval rectangle that says quite clearly: Content View. So all you can do is set the content view of the UITableViewCell in IB, and setting the background color property of the content view does not produce the color change in the table. As the docs say, UITableViewCells are complex beasts with many parts with a lot going on behind the scenes in terms of the UITableView adjusting it and fiddling with it.

Solution 8 - Ios

It works perfectly, if you pick the tableview and set background to for example red the whole table will be red. maybe you do not linked the table or something

hope it helped

Solution 9 - Ios

(try this)

Apple's IB's design is generic to be applied to multiple kinds of components, and is also targeted at a highly technical audience (developers). Hence it has not been designed (as an application) to fully customise the interface properly for each component/situation type. So in this case it is the case the ability to seemingly set the background-color is somewhat misleading.

Solution 10 - Ios

Y not, use setBackgroundColor

self.table.separatorColor=[UIColor whiteColor];
[table setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];

Solution 11 - Ios

You can partially do this in Interface Builder (probably added to Xcode recently):

  • Expand the cell in the Storyboard's Document Outline and select "Content View"
  • Set Content View background color in the Attributes Inspector
  • Select each content item in the cell and set their background colors to ClearColor

Still don't see a way to set the accessory background though.

Solution 12 - Ios

I did get this issue also. In IB you get the ability to change the background color of the cell when choosing cell. This is NOT correct. You should go to the document outline on the left of the storyboard and choose the content view inside the cell. Change the background of the content view. You should now get correct cell color.

Solution 13 - Ios

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:view];}

We need to change the color in this method.

Solution 14 - Ios

Try this it works for me:

- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
    tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGregView Question on Stackoverflow
Solution 1 - IoschrisView Answer on Stackoverflow
Solution 2 - IosShiprackView Answer on Stackoverflow
Solution 3 - IosocculusView Answer on Stackoverflow
Solution 4 - IosSubbuView Answer on Stackoverflow
Solution 5 - IosPrince Kumar SharmaView Answer on Stackoverflow
Solution 6 - IosfelipecamposclarkeView Answer on Stackoverflow
Solution 7 - IosBogatyrView Answer on Stackoverflow
Solution 8 - IosCsabiView Answer on Stackoverflow
Solution 9 - IosGregView Answer on Stackoverflow
Solution 10 - IosSplendidView Answer on Stackoverflow
Solution 11 - IosSymmetricView Answer on Stackoverflow
Solution 12 - IosOdd-ArneView Answer on Stackoverflow
Solution 13 - IosHemanshu LiyaView Answer on Stackoverflow
Solution 14 - Iosuser4919266View Answer on Stackoverflow