views:

521

answers:

1

I tried to override the second tableHeaderView. But it seems that the height of it in the method heightForHeaderInSection seems to be 0. Can't explain it, do I have to put it in a iVar because in the viewForHeaderInSection I can set the view without any problems.

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0)
    return @"Adding a new list";
else
    return @"Other lists";

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

if(section == 0) {
    return tableView.tableHeaderView;
} else {
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)] autorelease];
    view.backgroundColor = [UIColor redColor];

    return view;
}

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 0)
    return tableView.tableHeaderView.frame.size.height;
else
    return 30;
}
+1  A: 

I think you are confusing the table header and the header for each section, they are different. There is no "second tableHeaderView", a UITableView has one tableHeaderView and then depends on the viewForHeaderInSection and heightForHeaderInSection methods to place custom header views for each section, otherwise you just use titleForHeaderInSection to place text there.

Greg Martin
ok, but my approach is to have different section headers. First one should use the text and the second should use the text + an uibutton.
gabac
I'm not sure you can mix titleForHeaderInSection and viewForHeaderInSection, you must create custom views for each section if you want to use viewForHeaderInSection at all.
Greg Martin
jop got it. thx!
gabac
You can call `titleForHeaderInSection` from your own custom `viewForHeaderInSection`. So you can still use it to manage your titles. But there is no magic here and you have to stitch the 2 methods together yourself. It wont ask for a title if the delegate is setup to provide header views.
Squeegy