Josh Kahane
Josh Kahane

Reputation: 17169

UITableViewCell Background Color?

I have done a fair bit of googling on this and I can't find the right solution. I have a UITableView of which I want to change the colour of the background of the cells. I have found solutions to doing this but it only deals with cells which have content or data as it were.

I need to colour the background of those unused cells as well.

Upvotes: 3

Views: 2449

Answers (2)

jmstone617
jmstone617

Reputation: 5707

Even though UITableViewCell inherits from UIView, changing the backgroundColor property of the cell itself won't do anything. You need to change the background color of the cell's contentView, such as

cell.contentView.backgroundColor = [UIColor greenColor];

This is because the subviews of a UITableViewCell are actually subviews of the cell's contentView, because the contentView knows how to resize its subviews if a cell is put into editing mode; the cell itself doesn't know how to do that.

I'm not sure what you mean by unused cells. If you tell your tableView there are 10 cells and you only provide content for 8 of them, you'll still have 10 green cells, if that's what you mean by "unused".

Upvotes: 1

Nick Lockwood
Nick Lockwood

Reputation: 41005

Try setting the background colour of the tableView itself. Those "empty cells" at the bottom aren't really cells at all - they're just separator lines drawn over the background.

Upvotes: 6

Related Questions