rein
rein

Reputation: 33465

Default Header view for UITableView

I have a UITableView that is styled as a grouped tableView. Under certain circumstances I need to create a custom view for the header and under other circumstances I want the default one.

Creating a custom header is the easy part - simply use the delegate:

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

However, what do I return from this delegate if I want the default header view instead?

I've tried returning nil and this only works as expected when the table view is styled as Plain. If the table view is styled as Grouped then the default header disappears when I return nil.

How do I get the default header back taking into account that I need to sometimes have custom header views too?

Edit:

I think I've found the problem. In tableView:heightForHeaderInSection: I do the following:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ([myArray count] == 0) return 80;
    else return [tableView sectionHeaderHeight];
}

It seems that [tableView sectionHeaderHeight] returns an incorrect height for my header. It sets it to 10 but it should be about 30.

Upvotes: 5

Views: 5815

Answers (3)

Daniel Klöck
Daniel Klöck

Reputation: 21157

Actually, if you want the default header, you should be using following delegate method:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"my title text";
}

Upvotes: 0

Emmanuel
Emmanuel

Reputation: 126

The correct answer to the above questions is:

To return the default view return nil

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   return nil;
}

To return the default header height return -1

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

That worked for me !

Upvotes: 11

rein
rein

Reputation: 33465

Answered my own question in the edit. I needed to return a hard-coded height for the section header.

Upvotes: 0

Related Questions