Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Not Respecting heightForHeaderInSection/heightForFooterInSection?

I have a UITableView where in some instances, certain sections have zero rows. My goal is that when this is true, I don't want any wasted space in the table view, it should look like there's no data.

The problem I'm having is with the header and footer for the sections, which are showing even if there's no row and despite me overriding the delegate method to return 0.0f.

Here's what it looks like - you can see the ~20p of gray space at the top there, headers and footers of about 10p each for a section with 0 rows.

alt text
(source: hanchorllc.com)

Here's my pseudo code:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
     if ([section hasRow]) {
          return 10.0f;
     } else {
          return 0.0f;
     }
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
     if ([section hasRow]) {
          return 10.0f;
     } else {
          return 0.0f;
     }
}

I have verified that these methods are being called and that the proper execution path is taking place.

One wrinkle - this view controller is using a XIB and that UITableView has the section header and footer values set at 10.0 (default), though I thought that was overriden by the delegate method, if implemented.

This is an app targeting 3.0.

What am I doing wrong?

like image 836
Hunter Avatar asked Sep 07 '25 15:09

Hunter


1 Answers

This is a bit tricky, as 0.0f value is not accepted. but anything close enough to zero will do the trick. If You decide not to be pixel perfect and want the round numbers, then 1.0f will do almost the same, as 1px difference in height will be fairly noticable.

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 1 )
        return 0.000001f;
    else return 44.0f; // put 22 in case of plain one..
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001f; //removing section footers
}
like image 181
Juris V Avatar answered Sep 10 '25 06:09

Juris V