- January 2006 (1)
- January 2007 (1)
- July 2007 (8)
- August 2007 (3)
- September 2007 (3)
- October 2007 (2)
- November 2007 (3)
- January 2008 (5)
- February 2008 (4)
- March 2008 (1)
- April 2008 (5)
- June 2008 (3)
- July 2008 (2)
- August 2008 (1)
- September 2008 (6)
- November 2008 (3)
- December 2008 (1)
- January 2009 (4)
- March 2009 (1)
- April 2009 (14)
- May 2009 (9)
- June 2009 (7)
- July 2009 (6)
- August 2009 (4)
- September 2009 (4)
- October 2009 (2)
- November 2009 (23)
- December 2009 (23)
- January 2010 (4)
- February 2010 (3)
- March 2010 (2)
- May 2010 (3)
- July 2010 (4)
Changing Background Color and Section Header Text Color in a Grouped-style UITableView
While working on an iPhone application, I decided I wanted to change the colors of the background and section headers of a UITableView with the UITableViewStyleGrouped style. It took a lot more work than I expected, so I'm sharing what I learned with anyone else who needs to do this.
To review: when you create a table view with the grouped style, each section of the table shows up as a rounded rectangle, section titles are displayed as dark gray text between the rectangles, and the background is gray. I wanted the background to be a pale pastel color, so I looked into how to change that.
Of course, I first looked at the options in Interface Builder. A table view has a background color that can be set in IB, but setting that didn't accomplish anything.
With a bit of Googling, I learned that the way to give a table view a background color is to set the background color to [UIColor clearColor], and let the color (or image) of whatever is behind the table view show through. So, I set my window's backgroundColor to the color I wanted, and then added this to my table view's controller class:
#define TableViewTag 8888
- (void)viewDidLoad {
[super viewDidLoad];
// Make table view's background transparent to allow window background to be visible
UITableView *tableView = (UITableView *)[self.view viewWithTag:TableViewTag];
tableView.backgroundColor = [UIColor clearColor];
}
In Interface Builder, I set the tag of the table view to 8888. Another way to do this would be to have an outlet for the table view, but I didn't want to do that in this particular case.
So, that gave the table the background color I wanted, but I then noticed that the dark-gray section titles didn't look good against that color. I started looking for some sort of "sectionHeaderTextColor" property on the table view, but of course, there was no such thing.
After more Googling, I concluded that there was no way to just set the color. What you have to do is provide your own custom section header view containing a text label. With that, you can set whatever colors you want. So, I added these implementations of UITableViewDataSource protocol methods to my table view controller:
#define SectionHeaderHeight 40
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([self tableView:tableView titleForHeaderInSection:section] != nil) {
return SectionHeaderHeight;
}
else {
// If no section header title, no section header needed
return 0;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(20, 6, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green
saturation:1.0
brightness:0.60
alpha:1.0];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];
return view;
}
I also added a suitable implementation of tableView:titleForHeaderInSection:, and everything worked.
If there is a simpler way to do this, I'd love to hear about it.
Comments
If you have a lot of table
If you have a lot of table headers I'd suggest you find some way to cache these views and it could get costly recreating them as you scroll.
very helpful
Thank you!
You are a saviour
Thanks a lot.. ...
Just super! Simply copied and
Just super!
Simply copied and pasted into my TableViewController and it worked like a charm.
You saved me tons of time!
Thanks
Buggy colors
I've been trying to set the background color on a grouped UITableView programmatically, which sort-of worked, but that led to all sorts of weird caching bugs whenever I changed colors and orientations. Setting [UIColor clearColor] with an underlying view for the actual color did the trick, so thanks. :)
Thanks... you saved me a days
Thanks... you saved me a days headscratching! Works great.
Thanks!
Thanks for figuring this out and sharing! I looked all over to find out how to do this and this was the only place I found that even mentioned changing the color of the section headers.
Worked like a charm!
changes..
I think we don't require following
--------------------------------
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];
return view;
---------------------------------
instead we can just put:
return label;
isn't it?
...
That didn't work for me when I tried it. I had to wrap the label within the other view to get the size and margins to look right.
Thanks
Thanks a lot, you saved my time. I was just about to start experiments on this... but i thought i should google it first and found you at first link in google search...
Nice ...