Shabbir Panjesha
Shabbir Panjesha

Reputation: 438

UITableView cell background color

how to set different background colors for cells in a UITableView (specifically rainbow color for seven cells)

Upvotes: 20

Views: 26600

Answers (8)

albertamg
albertamg

Reputation: 28572

Set the backgroundColor property:

cell.backgroundColor = [UIColor redColor];

Note that the backgroundColor must be set in the tableView:willDisplayCell:forRowAtIndexPath: method (from UITableViewCell reference):

Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it.

Use the indexPath parameter to achieve the rainbow effect.

Upvotes: 83

kraftydevil
kraftydevil

Reputation: 5246

You may arrive at the conclusion that 'willDisplayCell' is still not working like I did at first. The reason it didn't work for me initially was because I wasn't using colors properly.

Don't forget that when using custom colors you need to divide by 255.0f:

[UIColor colorWithRed:44/255.0f green:50/255.0f blue:65/255.0f alpha:1];

Something like the following will result to a white cell:

//DON'T DO [UIColor colorWithRed:44 green:50 blue:65 alpha:1];

making it look like it's not doing anything when it really is setting the background to white.

Upvotes: 0

Raphael Oliveira
Raphael Oliveira

Reputation: 7841

You can set it like so:

cell.contentView.backgroundColor = [UIColor colorWithRed:249.0/255 green:237.0/255 blue:224.0/255 alpha:1.0];

Upvotes: 2

JLundell
JLundell

Reputation: 1610

If you've subclassed UITableViewCell, you can reliably set self.backgroundColor in -layoutSubviews. At least in my experience, this works in the odd cases where tableView:willDisplayCell:forRowAtIndexPath: does not.

Upvotes: 1

NiKKi
NiKKi

Reputation: 3296

Do not forget to set background color of your tableView to clearColor. Otherwise the clearColor of cell will not be displayed as the background color of tableView will be whiteColor by default. And even when the cell color turns to clearColor whiteColor will be displayed because the tableView background color is still whiteColor. Remenber.

Upvotes: 2

gamozzii
gamozzii

Reputation: 3921

If you want to set cell color based on some state in the actual cell data object, then this is another approach:

If you add this method to your table view delegate:

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

Then in your cellForRowAtIndexPath method you can do:

if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
    cell.contentView.backgroundColor = [UIColor blueColor];
}

This saves having to retrieve your data objects again in the willDisplayCell method.

Upvotes: 10

5hrp
5hrp

Reputation: 2230

Try this

cell.backgroundVIew.backgroundColor = [UIColor orangeColor];

Upvotes: 1

user142019
user142019

Reputation:

Pretty sure UITableViewCell is a subclass of UIView, so:

cell.backgroundColor = [UIColor blueColor];

For awful rainbow colors, here is an example:

static NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor yellowColor], etc..., nil];
cell.backgroundColor = [colors objectAtIndex:indexPath.row];

Upvotes: 1

Related Questions