I have a grouped UITableView where not all sections may be displayed at once, the table is driven by some data that not every record may have. My trouble is that the record
Here is the solution that worked for me
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
switch (section) {
case 1: case 3: case 5: return 20.0f; break;
default: return 0.01f; // for some reason 0 is not accepted - give something :)
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
switch (section) {
case 1: case 3: case 5: return 20.0f; break;
default: return 0.01f; // for some reason 0 is not accepted - give something :)
}
}
I solved the issue by setting tableView's dataSource and delegate where I initialize the tableView
Remember you might need to make tableView lazy to assign when initializing the tableView
lazy var tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.separatorStyle = .none
tableView.backgroundColor = .white
tableView.dataSource = self
tableView.delegate = self
return tableView
}()
In the above screenshot, is your numberOfSectionsInTableView:
method returning a value of 5? And then is your getRowCount:
method (called from numberOfRowsInSection:
) returning a value of 0 for those "missing" sections (e.g. 1, 3 and 4)?
If you don't want gaps in between, the likely solution is to only declare the number of sections you want to be visible. In your example above, you'd only want to return value of 3 from numberOfSectionsInTableView:
.
I believe that with section table views, each section must exist, and each section has buffer room for a header and a footer regardless of you putting text in them. What you need to do is remove the sections that aren't being used. In your example, you would remove section 1, 3, and 4. This would mean:
"section 0" is indexPath.section == 0
"section 2" is indexPath.section == 1
"section 5" is indexPath.section == 2
If you code in this fashion, the headers and footers will be removed because the nil cell is removed.
This isn't proper coding of course but I just wanted to give you an idea of the theology. Hope this helps.
This solution is based on user362178 solution. But keeps the labels of the sections intact:
Define somewhere at the top of your code:
#define HEIGHT_FOR_HEADER_AND_FOOTER 50
Add to the view did load:
self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = 0;
Add these methods:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
// Needed because if this does not return a value, the header label of the section will
// be at the top of the screen.
if (section == 0)
{
return HEIGHT_FOR_HEADER_AND_FOOTER;
}
if ([self tableView:tableView numberOfRowsInSection:section] == 0)
{
return 0;
}
else
{
return HEIGHT_FOR_HEADER_AND_FOOTER;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
// if last section
if (section == [self numberOfSectionsInTableView:tableView] - 1)
{
return HEIGHT_FOR_HEADER_AND_FOOTER;
}
return 0;
}
I had the same problem in iOS 7 and finally I solved this checking the Height for Header and Footer sections, as Ramesh said:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([[self.tableView objectAtIndex:section] count] == 0)
return 0.01f;
else
return 30.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01f;
}
It's curious, but if you set 0.f as the height the sections is showed with a default size and moves down the other sections.