UITableView : viewForHeaderInSection: not called d

2019-01-21 15:46发布

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?

12条回答
Root(大扎)
2楼-- · 2019-01-21 16:05

@rmaddy has misstated the rule, twice: in reality, tableView:viewForHeaderInSection: does not require that you also implement tableView:heightForHeaderInSection:, and also it is perfectly fine to call both titleForHeader and viewForHeader. 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.

查看更多
甜甜的少女心
3楼-- · 2019-01-21 16:06

Sometimes setting tableview.delegate or datasource = nil in the viewWillAppear: or viewDidAppear: methods can cause this issue. Make sure not to do this...

查看更多
走好不送
4楼-- · 2019-01-21 16:14

The trick is that those two methods belong to different UITableView protocols:

tableView:titleForHeaderInSection: is a UITableViewDataSource protocol method, where tableView:viewForHeaderInSection belongs to the protocol UITableViewDelegate (I wonder why, any comments?)

That means

  1. If you implement the methods but assign yourself only as the dataSource for the UITableView, tableView:viewForHeaderInSection implementation of yours will be ignored and will never be called.

  2. tableView:viewForHeaderInSection has a higher priority. If you implement both of the methods and assign yourself as both the dataSource and the delegate for the UITableView, you will return the views for section headers but your tableView:titleForHeaderInSection: will be ignored.

  3. 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 the tableView:viewForHeaderInSection to work correctly; so to be safe it is wise to implement this.

查看更多
虎瘦雄心在
5楼-- · 2019-01-21 16:17

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.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
         return 40;
}
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-21 16:17

It's worth briefly noting that if your implementation of tableView:heightForHeaderInSection: returns UITableViewAutomaticDimension, then tableView:viewForHeaderInSection: will not be called.

UITableViewAutomaticDimension assumes that a standard UITableViewHeaderFooterView will be used that is populated with the delegate method tableView:titleForHeaderInSection:.

From comments in the UITableView.h:

Returning this value from tableView:heightForHeaderInSection: or tableView:heightForFooterInSection: results in a height that fits the value returned from tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: if the title is not nil.

查看更多
爷的心禁止访问
7楼-- · 2019-01-21 16:19

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,

-(CGFloat)tableView:(UITableView *)tableView 
         estimatedHeightForHeaderInSection:(NSInteger)section
{
      return 44.0f;
}

Although I have checked both options

  1. Automatic Height Calculation
  2. Automatic Estimated Height Calculation

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

查看更多
登录 后发表回答