I would like to customize both the background and the border color of a grouped-style UITableView.
I was able to customize the background color by using the following:
tableView.contentView.backgroundColor = [UIColor greenColor];
But the border color is still something I don't know how to change.
How do I customize these two aspects of the grouped-style table view?
Best Answer
UPDATE: In iPhone OS 3.0 and later
UITableViewCell
now has abackgroundColor
property that makes this really easy (especially in combination with the[UIColor colorWithPatternImage:]
initializer). But I'll leave the 2.0 version of the answer here for anyone that needs it…It's harder than it really should be. Here's how I did this when I had to do it:
You need to set the UITableViewCell's backgroundView property to a custom UIView that draws the border and background itself in the appropriate colors. This view needs to be able to draw the borders in 4 different modes, rounded on the top for the first cell in a section, rounded on the bottom for the last cell in a section, no rounded corners for cells in the middle of a section, and rounded on all 4 corners for sections that contain one cell.
Unfortunately I couldn't figure out how to have this mode set automatically, so I had to set it in the UITableViewDataSource's -cellForRowAtIndexPath method.
It's a real PITA but I've confirmed with Apple engineers that this is currently the only way.
Update Here's the code for that custom bg view. There's a drawing bug that makes the rounded corners look a little funny, but we moved to a different design and scrapped the custom backgrounds before I had a chance to fix it. Still this will probably be very helpful for you: