Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Formatting of titleforHeaderInSection is not working

Dear Developers,

I am trying to change the colour of the headers for sections in my grouped tableview. I am using the following code;


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
NSString *headerText = [[plistFileName objectAtIndex:section]objectForKey:@"Header"];
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:10];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = headerText;
return headerText;
}


The headers are displayed for each section, but they are not showing the features that i have defined above in the UILabel (e.g. white in color and size 10 Font) - which makes me wonder whether the UILable code is being recognised?

What do i need to do to correct this?

j

macbook, Mac OS X (10.6.4)

Posted on Aug 10, 2010 9:25 AM

Reply
5 replies

Aug 10, 2010 11:08 AM in response to james_coleman01

Hi James -

Something like this should do what you want:

#pragma mark -
#pragma mark Table View Delegate Methods
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease];
sectionHeader.backgroundColor = [UIColor groupTableViewBackgroundColor];
sectionHeader.textAlignment = UITextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:16];
sectionHeader.textColor = [UIColor blueColor];
sectionHeader.text = [NSString stringWithFormat:@"Section %d", section];
return sectionHeader;
}
- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section {
return 40;
}

Note that the frame you specify for the label is ignored. The table view will set the frame based on the table view's width and the height you return as shown above.

- Ray

Aug 10, 2010 5:23 PM in response to RayNewbie

Nice one Ray,

I got it working by tweaking the code you posted. The amended code is below for other people that need a similar solution.

with thanks,

james


- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.textAlignment = UITextAlignmentLeft;
sectionHeader.font = [UIFont boldSystemFontOfSize:10];
sectionHeader.textColor = [UIColor whiteColor];
sectionHeader.text = [[plistArray objectAtIndex:section]objectForKey:@"Header"]; // <-- allows loading of sectionHeader text from a plist
return sectionHeader;
}
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
return 40;

Formatting of titleforHeaderInSection is not working

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.