I've set up the tableview with correct delegate and datasource linkages.. the reloadData method calls the datasource and the delegate methods except for viewForHeaderInSection:
.
Why is that so?
I've set up the tableview with correct delegate and datasource linkages.. the reloadData method calls the datasource and the delegate methods except for viewForHeaderInSection:
.
Why is that so?
Giving
estimatedSectionHeaderHeight
andsectionHeaderHeight
values fixed my problem. e.g.,self.tableView.estimatedSectionHeaderHeight = 100 self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
Here's what I've found (Swift 4) (thanks to this comment on another question)
Whether I used titleForHeaderInSection or viewForHeaderInSection - it wasn't that they weren't getting called when the tableview was scrolled and new cells were being loaded, but any font choices I made for the headerView's textLabel were only appearing on what was initially visible on load, and not as the table was scrolled.
The fix was willDisplayHeaderView:
I've just had an issue with headers not showing for iOS 7.1, but working fine with later releases I have tested, explicitly with 8.1 and 8.4.
For the exact same code, 7.1 was not calling any of the section header delegate methods at all, including:
tableView:heightForHeaderInSection:
andtableView:viewForHeaderInSection:
.After experimentation, I found that removing this line from my
viewDidLoad
made headers re-appear for 7.1 and did not impact other versions I tested:… so, there seems to be some kind of conflict there for 7.1, at least.
I had cut & paste the following two methods from a Swift 2 project into my Swift 3 project which were never called because in Swift 3 these methods must have "-" before the first parameter name.
The use of
tableView:viewForHeaderInSection:
requires that you also implementtableView:heightForHeaderInSection:
. This should return an appropriate non-zero height for the header. Also make sure you do not also implement thetableView:titleForHeaderInSection:
. You should only use one or the other (viewForHeader
ortitleForHeader
).Going off rmaddy 's answer, I was trying to hide the Header view and was returning 0.0f for "tableView:heightForHeaderInSection" and a 0 height View from
tableView:viewForHeaderInSection
.After changing from
return 1.0f
toreturn 0.0f
intableView:heightForHeaderInSection
, the delegate methodtableView:viewForHeaderInSection
was indeed called.Turns out my desired effect works without having to use "tableView:heightForHeaderInSection"; but this may be useful to others who are having an issue getting "tableView:heightForHeaderInSection" delegate method called.