I'm trying to change the height of a particular section header inside a table view. I tested with code below just to understand how tableView(_:heightForHeaderInSection:)
works.
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
print("DEBUG : ", section, tableView.sectionHeaderHeight)
return tableView.sectionHeaderHeight
}
Debug print below shows that the method is called multiple times, but that the header height is always 18.0 (which is default).
DEBUG : 4 18.0
DEBUG : 4 18.0
DEBUG : 0 18.0
DEBUG : 0 18.0
DEBUG : 1 18.0
DEBUG : 1 18.0
DEBUG : 2 18.0
...
DEBUG : 3 18.0
Now, as soon as I use 18.0 as fix value for return (for testing purpose), the vertical extend of the table view is visually compressed, id est the sections are closer together, and the entire UI looks therefore different. It seems that the space between the sections is reduced, since the header of the first section is (vertically) only half visible.
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
print("DEBUG : ", section, tableView.sectionHeaderHeight)
return 18.0
}
How is that possible?
Possibly a bug?
--- UPDATE --- (13.06.2019)
My question was based on the intention to hide a section (2) including header. I realized that tableView.sectionHeaderHeight
was the wrong property to use. I should have used super.tableView(tableView, heightForHeaderInSection: section)
.
The code below works as desired:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if (section == 2) && myCondition {
return CGFloat.leastNonzeroMagnitude
}
return super.tableView(tableView, heightForHeaderInSection: section)
}
Nevertheless, I don't know where the 18.0
(see above) comes from since I don't use .xib files.
BTW super.tableView(tableView, heightForHeaderInSection: section)
always returns -1
. I believe this makes iOS decide on its own which heigth to choose. The manually set 18.0
(for testing) made the header shrink since the iOS chosen automatic value for the header height is higher.
I didn't find a property to print this value (must be around 30.0
- a wild guess, nothing more).