views:

1124

answers:

4

Hi,

How can I set the background color of a cell in UITableView?

Thanks.

+1  A: 

Update

Apparently the existing UITableViewCell framework makes it very difficult to change the background color of a cell and have it work well through all its state changes (editing mode, etc.).

A solution has been posted at this SO question, and it's being billed on several forums as "the only solution approved by Apple engineers." It involves subclassing UITableViewCell and adding a custom view for the subclassed cell's backgroundView property.

Original post - this solution doesn't work fully, but may still be useful in some situations

If you already have the UITableViewCell object, just alter its contentView's backgroundColor property.

If you need to create UITableViewCells with a custom background color, the process is a bit longer. First, you'll want to create a data source for your UITableView - this can be any object that implements the UITableViewDataSource protocol.

In that object, you need to implement the tableView:cellForRowAtIndexPath: method, which returns a UITableViewCell when given an NSIndexPath for the location of the cell within the table. When you create that cell, you'll want to change the backgroundColor property of its contentView.

Don't forget to set the dataSource property of the UITableView to your data source object.

For more info, you can read these API docs:

Note that registration as an Apple developer is required for all three of these links.

Tim
I have added this line in cellForRowAtIndexPath: method cell.backgroundView.backgroundColor = [UIColor orangeColor];. It still shows black text on white background
ebaccount
I have tried cell.backgroundColor= [UIColor orangeColor]; this does not work either
ebaccount
As Rob said below, you want the contentView, not the backgroundView. My bad; edited to correct.
Tim
cell.contentView.backgroundColor=[UIColor orangeColor]; does not work either. It shows orange color at the two end of the cell, but I want orange color as a background. Could you let me know how can I do that?
ebaccount
A: 

The backgroundView is all the way on the bottom. It's the one that shows the rounded corners and the edges. What you want is the contentView which is on top of the backgroundView. It covers the usually white area of the cell.

Rob Napier
Actually, I want to achieve the effect where one cell of the table view will have blue background, the next one will have white, the next one will have blue again, and then white and so on... could you let me know how can I do that?
ebaccount
cell.contentView.backgroundColor=[UIColor orangeColor]; does not work either. It shows orange color at the two end of the cell, but I want orange color as a background. Could you let me know how can I do that?
ebaccount
A: 

This works perfect for me: NSEnumerator enumerator = [cell.subviews objectEnumerator]; id anObject; while (anObject = [enumerator nextObject]) { if( [anObject isKindOfClass: [ UIView class] ] ) ((UIView)anObject).backgroundColor = [UIColor lightGrayColor]; }

slatvick
A: 

The version I wrote will work in iPhone 3.0 or higher and fallback to a white background otherwise.

In your viewDidLoad method of the UITableViewController we add the following:

self.view.backgroundColor=[UIColor clearColor];
// Also consider adding this line below:
//self.tableView.separatorColor=[UIColor clearColor];

When you are creating your cells (in my code this is my tableView:cellForRowAtIndexPath:) add the following code:

cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"code_bg.png"]];
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 3.0)
{
     [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
}
Casebash