Skip navigation
This discussion is archived

Formatting of titleforHeaderInSection is not working

6362 Views 5 Replies Latest reply: Aug 10, 2010 5:23 PM by james_coleman01 RSS
james_coleman01 Level 1 Level 1 (35 points)
Currently Being Moderated
Aug 10, 2010 9:25 AM
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?

macbook, Mac OS X (10.6.4)
  • sptrakesh Level 4 Level 4 (1,785 points)
    No, the label code you have will not be recognised. The method expects only a string as response, so anything else you do in that method will not be displayed anywhere (unless you add it to the view hierarchy, but that is another issue).
    Macbook Pro, Mac OS X (10.6.4), Solaris 10
  • RayNewbie Level 5 Level 5 (6,810 points)
    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
    iMac, Mac OS X (10.5.8)
  • sptrakesh Level 4 Level 4 (1,785 points)
    I don't think you can achieve what you want with the standard UITableView. The replacement table view in the three20 framework may give you more control (I have not played around with it a lot, but I know their table is more customisable).
    Macbook Pro, Mac OS X (10.6.4), Solaris 10


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.