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?
I have been having problems with this and tried lots of combinations of things as I noticed that for some cells it worked fine but not for others.
Strangely I found out that it is possible to set the cell.backgroundColor to lightGrayColor and all works perfectly - but blueColor caused me problems of not updating the outside edges.
Unless it is really important to use green - perhaps you might want to try this. It might be that this is a feature to get people to use only grey colours when indicating a cell is selected.
To change the table view border color:
In.h:
In .m:
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:
First of all thanks for this code. I have made some drawing changes in this function to remove corner problem of drawing.
One thing I ran into with the above CustomCellBackgroundView code from Mike Akers which might be useful to others:
cell.backgroundView
doesn't get automatically redrawn when cells are reused, and changes to the backgroundView's position var don't affect reused cells. That means long tables will have incorrectly drawncell.backgroundViews
given their positions.To fix this without having to create a new backgroundView every time a row is displayed, call
[cell.backgroundView setNeedsDisplay]
at the end of your-[UITableViewController tableView:cellForRowAtIndexPath:]
. Or for a more reusable solution, override CustomCellBackgroundView's position setter to include a[self setNeedsDisplay]
.Thanks for this super helpful post. In case anyone out there (like me!) wants to just have a completely empty cell background in lieu of customizing it through images/text/other content in IB and cannot figure out how the hell to get rid of the dumb border/padding/background even though you set it to clear in IB... here's the code I used that did the trick!
Hopefully that'll help someone else! Seems to work like a charm.