开发者

Customize header section for UITableViewController

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. M开发者_运维百科y 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.


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;
}


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...


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;
  }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜