Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 891
rein Avatar asked Nov 18 '09 19:11

rein


People also ask

How can I make the Footerview always stay at the bottom in Uitableviewcontroller?

If you need to make the footer view fixed at the bottom then you can not use a TableViewController . You will have to use UIViewController , put your tableView as a subview. Put the footer also as another subview and its done.


1 Answers

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 !

like image 155
Emmanuel Avatar answered Oct 18 '22 00:10

Emmanuel