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?
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];
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];
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
You should always provide changes for cells in tableView:willDisplayCell:forRowAtIndexPath:
instead of the tableView:cellForRowAtIndexPath:
method, as per Apple Documentation.
This Works !!
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];
Make sure before defining a cell color your backgroundView
is null
cell.backgroundView = nil;
cell.backgroundColor = [UIColor redColor];
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.
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
(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.
Y not, use setBackgroundColor
self.table.separatorColor=[UIColor whiteColor];
[table setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
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.
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.
-(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.
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"]];
}