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
It works for me:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
tableView.sectionHeaderHeight = (section == 0 ? 10 : 0);
return tableView.sectionHeaderHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
tableView.sectionFooterHeight = (section == 0 ? 20 : 4);
return tableView.sectionFooterHeight;
}
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?)
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
}