5 Replies Latest reply: Aug 10, 2010 5:23 PM by james_coleman01
james_coleman01 Level 1 Level 1 (35 points)
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)
  • 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).
  • james_coleman01 Level 1 Level 1 (35 points)
    ok - so what do i have to do / where can i find out how to make this work?

    j
  • 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
  • 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).
  • james_coleman01 Level 1 Level 1 (35 points)
    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;