tableView section headers disappear SWIFT

混江龙づ霸主 提交于 2019-11-28 19:08:45
Mason Ballowe

I found an answer in the console output. Use this code in the header function:

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

Do not return your headerCell, or your reusable identifier. Return the reuseIdentifier.contentView. For me it's: return headerCell!.contentView.

Just to add, I was baffled for WAY longer than I should have been as to why I couldn't refer to the contentView of my cell, when I could quite clearly see it was there. My custom class (using UITableViewCell rather than UITableViewHeaderFooterView) would return a fatal error each time. Therefore make sure any custom styling is setup under UITableViewHeaderFooterView class like:

class CustomHeaderCell: UITableViewHeaderFooterView {

You will also need to register the resuableIdentifer like this:

tableView.registerNib(UINib(nibName: "HeaderCell", bundle: nil), forHeaderFooterViewReuseIdentifier: "CellHeader")

Then this bad boy:

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerCell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("CellHeader") as! CustomHeaderCell!
    return headerCell!.contentView
}

Since I'm not at 50 reputation yet, I can't comment on the previous answer, so I apologize for listing this as another answer.

Returning the ContentView will make the function work but will remove all formatting done to the reuseIdentifier (headerCell)

headerCell.backgroundColor = UIColor.cyanColor()

This will NOT provide a Cyan color to your headerCell

To fix this, just add the ".contentView" to your formatting lines

headerCell.contentView.backgroundColor = UIColor.cyanColor()
Frans

Table view headers in 2 tables disappeared when I converted my app to IOS 10 - I found the reason in Apple developer API documentation on table headers. When I added the following, the missing headers reappeared!

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
return 44 // where 44 is the header cell view height in my storyboard
}

I had the same bug because I was returning a cell using dequeue method instead of a UITableViewHeaderFooterView.

Solution:

  • Add a view outside of the view hierarchy
  • Set the type to UITableViewHeaderFooterView
  • Customize
  • Link to an @IBOutlet
  • In func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? return the outlet

Common pitfalls:

Don't forget to set the header sizes Don't forget to set the outlet as strong.

Sonu VR

You could wrap the tableviewcell inside a UIView

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

let containerView = UIView()
guard let headerCell = tableView.dequeueReusableCell(withIdentifier: "MyHeaderView") as? MyHeaderView else { fatalError(" Failed to load MyHeaderView") }
containerView.addSubview(headerCell)
return containerView
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!