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条回答
做自己的国王
2楼-- · 2019-01-21 15:53

Giving estimatedSectionHeaderHeight and sectionHeaderHeight values fixed my problem. e.g., self.tableView.estimatedSectionHeaderHeight = 100 self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension

查看更多
欢心
3楼-- · 2019-01-21 15:53

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:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let header = view as? UITableViewHeaderFooterView {
        header.textLabel?.font = UIFont(name: yourFont, size: 42)
    }
}
查看更多
太酷不给撩
4楼-- · 2019-01-21 15:54

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: and tableView: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:

// _Removing_ this line _fixed_ headers on 7.1
self.tableView.estimatedSectionHeaderHeight = 80;

… so, there seems to be some kind of conflict there for 7.1, at least.

查看更多
Evening l夕情丶
5楼-- · 2019-01-21 15:54

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.

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44.0
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: B2BTrolleyHeaderFooterView.reuseIdentifier) as! B2BTrolleyHeaderFooterView        
    return headerView
}
查看更多
虎瘦雄心在
6楼-- · 2019-01-21 16:00

The use of tableView:viewForHeaderInSection: requires that you also implement tableView:heightForHeaderInSection:. This should return an appropriate non-zero height for the header. Also make sure you do not also implement the tableView:titleForHeaderInSection:. You should only use one or the other (viewForHeader or titleForHeader).

查看更多
Bombasti
7楼-- · 2019-01-21 16:00

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 to return 0.0f in tableView:heightForHeaderInSection, the delegate method tableView: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.

查看更多
登录 后发表回答