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?
@rmaddy has misstated the rule, twice: in reality,
tableView:viewForHeaderInSection:
does not require that you also implementtableView:heightForHeaderInSection:
, and also it is perfectly fine to call bothtitleForHeader
andviewForHeader
. I will state the rule correctly just for the record:The rule is simply that
viewForHeader
will not be called unless you somehow give the header a height. You can do this in any combination of three ways:Implement
tableView:heightForHeaderInSection:
.Set the table's
sectionHeaderHeight
.Call
titleForHeader
(this somehow gives the header a default height if it doesn't otherwise have one).If you do none of those things, you'll have no headers and
viewForHeader
won't be called. That's because without a height, the runtime won't know how to resize the view, so it doesn't bother to ask for one.Sometimes setting
tableview.delegate
ordatasource = nil
in theviewWillAppear:
orviewDidAppear:
methods can cause this issue. Make sure not to do this...The trick is that those two methods belong to different UITableView protocols:
tableView:titleForHeaderInSection:
is aUITableViewDataSource
protocol method, wheretableView:viewForHeaderInSection
belongs to the protocolUITableViewDelegate
(I wonder why, any comments?)That means
If you implement the methods but assign yourself only as the
dataSource
for theUITableView
,tableView:viewForHeaderInSection
implementation of yours will be ignored and will never be called.tableView:viewForHeaderInSection
has a higher priority. If you implement both of the methods and assign yourself as both thedataSource
and thedelegate
for theUITableView
, you will return the views for section headers but yourtableView:titleForHeaderInSection:
will be ignored.For research purposes, I have removed my
tableView:heightForHeaderInSection:
; it worked fine and didn't seem to affect the procedures above. But the comment doc states that it is required for thetableView:viewForHeaderInSection
to work correctly; so to be safe it is wise to implement this.You should implement
tableView:heightForHeaderInSection:
and set the height for the header >0.This delegate method goes along with the
viewForHeaderInSection:
method.I hope this helps.
It's worth briefly noting that if your implementation of
tableView:heightForHeaderInSection:
returnsUITableViewAutomaticDimension
, thentableView:viewForHeaderInSection:
will not be called.UITableViewAutomaticDimension
assumes that a standardUITableViewHeaderFooterView
will be used that is populated with the delegate methodtableView:titleForHeaderInSection:
.From comments in the
UITableView.h
:Same issue occured with me but as I was using automatic height calculation from xCode 9, I cannot give any explicit height value as mentioned above. After some experimentation I got solution, we have to override this method as,
Although I have checked both options
from storyboard as apple says, but still I got this weird error.
Please Note: This Error was shown only on IOS-10 version not on IOS-11 version. Maybe it's a bug from xCode. Thanks