tableView section headers disappear SWIFT

19,156

Solution 1

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.

Solution 2

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
}

Solution 3

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()

Solution 4

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
}

Solution 5

You could wrap the TableViewCell inside an UIView

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  let containerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50)) // 50 = Header height

  guard let headerCell = tableView.dequeueReusableCell(withIdentifier: "MyHeaderView") as? MyHeaderView else { fatalError(" Failed to load MyHeaderView") }
  headerCell.frame = containerView.bounds
  containerView.addSubview(headerCell)

  return containerView
}
Share:
19,156
Mason Ballowe
Author by

Mason Ballowe

Updated on June 02, 2022

Comments

  • Mason Ballowe
    Mason Ballowe about 2 years

    I have a tableView set up so that when a cell is touched, it expands in height to reveal more information. The tableView has 5 sections.

    I have a bug: when a cell expands, all headersCells below that cell go invisible. The console outputs the following: "[31233:564745] no index path for table cell being reused"

    In my storyboard I have 2 custom cells : "myCell" for the data bearing cells, and "headerCell" for the headers.

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
       let thisGoal : GoalModel = goalArray[indexPath.section][indexPath.row]
    
    
        if self.currentPath != nil && self.currentPath == indexPath  {
            self.currentPath = nil
        } else {
            self.currentPath = indexPath
        }
        tableView.beginUpdates()
        tableView.endUpdates()
    }
    

    If I enter tableView.reloadData() in between the begin/end updates, it functions properly, although the header background turns black, and loses animation.

    I have all of the stuff for headers declared in: func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

    what am I missing? I'd really like the tableView to still have animations, and keep the background clearColor().

    Thanks in advance. I did read through the objective C answers, but couldn't get them to work for me. I'd really appreciate some SWIFT help.

    I think the problem is the no index path for table cell being reused.

  • Artem Mostyaev
    Artem Mostyaev over 8 years
    I get transparent header view in such case.
  • Led
    Led almost 8 years
    no need to add UITableViewHeaderFooterView if the class is extended in UITableViewController. thank you for the headerCell!.contentView answer
  • eMdOS
    eMdOS over 7 years
    What if you want to add buttons or gestures to the cell? It won't work, 'cause contentView won't know anything about it.
  • user2749248
    user2749248 over 7 years
    yeah it doesnt work if i have a button... did you find a solution for this eMdOS?
  • Lohith Korupolu
    Lohith Korupolu almost 7 years
    All my views go invisible when I return header.contentView
  • Shrikant K
    Shrikant K almost 7 years
    Add tap gesture on contentView not on cell/view iteself, then it will work.
  • Satheesh
    Satheesh over 6 years
    It is not compulsory to use UITableViewHeaderFooterView, I am using UITableViewCell class but the key was to return the cell.contentView rather the cell itself. Returning the whole cell was causing the issue for me.
  • Grease
    Grease over 6 years
    adding .contentView saved my day
  • Sagar Daundkar
    Sagar Daundkar about 6 years
    Thanks for solving my issue, but can someone explain why it was happening?
  • user1046037
    user1046037 about 6 years
    The alternative is to create your own label in your custom cell subclass and that way you can return the view instead of the content view
  • WorieN
    WorieN about 6 years
    This is the best answer! Never make your headers like a cells guys!
  • Christopher Pickslay
    Christopher Pickslay over 5 years
    This seems to only be the case in iOS 10, not necessary in 11/12
  • Rishab
    Rishab over 5 years
    Hahaha. It worked. Can't believe but it worked. Screw Apple.
  • Mitesh Dobareeya
    Mitesh Dobareeya about 5 years
    Returning content view removed my formatting of cell i.e corner radius. Any solution ?
  • dev_exo
    dev_exo almost 5 years
    I'll use a custom UIView inside the cell and put all the controls/other views in it instead of the cell. And use that custom UIView as the header.