+3  A: 

In a "grouped" UITableView on the iPhone it will still render a minimum height for header and footer, effectively ignoring your code to set it to zero. It is not linked to the XIB default.

This is because a zero height section header or footer would look very odd. Therefore Apple has decreed that a header height cannot be set to 0. And therefore multiple "empty" sections will render oddly as per your screenshot.

I'm afraid it's all because you're going the wrong way about clearing the header size when there are no data rows; instead you should not be calling any methods for empty sections. It is a bad technique because essentially the iPhone is having to call more methods than it ought to, and also you render more headers than you want to (usually - sometimes people want to leave the blank headers there, for example for drag and drop).

So, for example, let's imagine that you have a number of sections, but only some of them have more than one row in (perhaps based on user settings/filters).

The wrong way of implementing it is:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return totalNumberOfPossibleSections;
}

and then for each section you have no result for:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (![section hasRow]) return 0;
}

and

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

The correct way of implementing it is:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return numberOfSectionsWhichHaveAtLeastOneRowInThem;
}

and then each section will have at least one result. That way, sections with no data aren't even rendered, methods aren't even called for them. Note: my two variable names are made up to convey what they'll contain! They're not special Apple variables...

Hope that helps!

h4xxr
Yes, this sounds reasonable.I've gotten into the habit of declaring enum constants for my table section layouts and I was trying to force this particular controller into that design, which doesn't work very well with an unknown number of rows in sections.Odd that Apple's documentation doesn't indicate anywhere (that I can find) that it won't accept 0.0 as a value, especially since you *can* set 0.0 in the XIB (and yes, it looks really weird). I may file a doco bug as it should at least be noted that the value has to be greater than 0.0f.Cheers.
Hunter
It does indeed look weird - I also have not seen it in any of the documentation so probably worth filing that as a bug.
h4xxr
This is a good idea, but I had troubles implementing it. I have switch statements for section in a bunch of my tableview methods. Returning a variable number of sections depending on the data contents really messes up this mapping and I can't think of an elegant (or even easy) way to deal with it.
blindJesse
A: 

Actually, with me it is happening other way round. I have assigned section header to be 10 in xib, but for the first section, I want a header of size header. I am using this method in my UITableViewCotroller

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==0)
     return 125;
    return tableView.rowHeight;
}

But the section header heights are not changed, even though the method is getting called.

saurabh
A: 

I had a similar problem: I was loading a UITableView from a XIB (same as you), and supplied a 0 height for some section footers with tableView:heightForFooterInSection (same as you), but the value was ignored.

The fix was simple: also set the footer height to 0.0 in the XIB. (In Interface Builder select the Table View, press Command-3 to view the Size Inspector, look for the footer height field near the top)

Once that was done it obeyed the custom footer height as expected. (Perhaps it treats the XIB footer height as a minimum?)

mindfulbear