Customize header section for UITableViewController

26,765

Solution 1

You can set explicitly the delegate:

 detailViewController.tableView.delegate = detailViewController;

Or you can do it in the controller initial function.

EDIT: your init method should conform to the canonical init. Furthermore, it seems to me that you have not created your UITableView. Try and use this code:

- (id)initWithStyle:(UITableViewStyle)style { 
    if ((self = [super initWithStyle:style])) {
        self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
        self.tableView.autoresizingMask =  UIViewAutoresizingFlexibleWidth  UIViewAutoresizingFlexibleHeight;
        self.tableView.delegate = self;
    }
    return self;
}

Of course, you could also do all of this in a nib file...

Solution 2

This question is an older one but I wanted to share my code. I'm using a usual table cell view for my section headers. I have designed it with interface builder and implemented the following delegate method.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Header"];
  cell.textLabel.text = @"test";
  return cell;
}

Solution 3

Here is how you get the barebones section view up using the UITableViewDelegate methods:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40.0)];
    header.backgroundColor = [UIColor grayColor];

    UILabel *textLabel = [[UILabel alloc] initWithFrame:header.frame];
    textLabel.text = @"Your Section Title";
    textLabel.backgroundColor = [UIColor grayColor];
    textLabel.textColor = [UIColor whiteColor];

    [header addSubview:textLabel];

    return header;
 }

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  {
    return 40.0;
  }

Solution 4

You could try this: In your ProjectDetails.h declare a UIView *tableHeader and also an accessor method - (UIView *)tableHeader;. Then in the implementation file:

- (UIView *)tableHeader {
    if (tableHeader)
        return tableHeader;

    tableHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
    // addlabel
    return tableHeader;
}

In viewDidLoad, call: self.tableView.tableHeaderView = [self tableHeader];

I don't believe you'll need to use the heightForHeaderInSection method.

Share:
26,765
Derek Wildstar
Author by

Derek Wildstar

Updated on August 16, 2022

Comments

  • Derek Wildstar
    Derek Wildstar almost 2 years

    I'll need to customize the header section of a UITableViewController where for each sections a different header text is returned (getting data from datasource as well). This is accomplished using the following:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        NSArray *temp = [listOfMBeans allKeys];
        DLog(@"MBean details: %@", temp);
        NSString *title = [temp objectAtIndex:section];
        DLog(@"Header Title: %@", title);
        return title;
    }; 
    

    This works well and I can see the expected output. However I need to change also the font size of text and after looking at similar questions I've implemented the following:

    - (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        DLog(@"Custom Header Section Title being set");
        UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];  
    
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
        label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:14];
    
        [headerView addSubview:label];
        return headerView;
    }
    
    - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 44.0;
    }
    

    However it seems that the code is never called. My understanding was that UITableViewController is setting by default itself as delegate but it seems I'm wrong.

    The UITableViewController is created in this way (as part of hierarchical data):

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        ProjectDetails *detailViewController = [[ProjectDetails alloc] initWithStyle:UITableViewStyleGrouped];
        detailViewController.project = [listOfMetrics objectAtIndex:indexPath.row];
    
        // Push the detail view controller.
        [[self navigationController] pushViewController:detailViewController animated:YES];
        [detailViewController release]; 
    }
    

    What changes, I should make to make this working? Thanks.

  • Derek Wildstar
    Derek Wildstar almost 13 years
    Hi Mark, I've tried this but although I can see that the method is called section header is not changed :( I still see my section text that is coming out from the deafult (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  • Derek Wildstar
    Derek Wildstar almost 13 years
    Ok Definetively I got lost at this point :( I've tried both solution but no luck. Into ProjectDetails.m I've added this - (id)initWithStyle:(UITableViewStyle)style { UITableViewController *tvc = [super initWithStyle:style]; tvc.tableView.delegate = self; return tvc; } but still cannot see - (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  • Mark Leonard
    Mark Leonard almost 13 years
    If you're trying to create custom headers for multiple sections in the tableView, then I may have misunderstood your question.
  • Derek Wildstar
    Derek Wildstar almost 13 years
    Yes, for each section I've an header with different text (coming out from datasource as well). I'll edit the original question to make this point clear.
  • Derek Wildstar
    Derek Wildstar almost 13 years
    Ahh that's the trick... I'll need to create my own UITableView and not using the one generated by defaults. Thank you very much!
  • petert
    petert almost 12 years
    Hope I've corrected the code typo's above. Thanks for answer.
  • eugene
    eugene over 11 years
    why are you using cell for header view? Are you using two types of cells (header/body) for some kind of nested list? I wonder how sns apps implement nested list view and if you are doing similar sort of thing?
  • cocoafan
    cocoafan over 11 years
    @Eugene because you can design cells in IB. Yes I have nested lists and at least two cell prototypes 'header' and 'item'. I'm not sure how others implement this approach.
  • Parrots
    Parrots almost 11 years
    One interesting side effect of doing it this way that I just ran into - you appear to get colored borders around the cell (even when the table has separator set to none). Manually creating and retuning a UIView doesn't have this issue. Note: I only noticed this because I had dark headers.
  • maxday
    maxday over 10 years
    Since you've set the label frame equals to the hearder frame, the header backgroundColor instruction is useless since it's replaced by the textLabel backgroundColor one
  • Jayprakash Dubey
    Jayprakash Dubey over 10 years
    @cocoafan ...Nothing is getting display in viewForHeaderInSection.
  • cocoafan
    cocoafan over 10 years
    @JayprakashDubey Did you also set the identifier to "Header"?