tableView section headers disappear SWIFT

Mason Ballowe picture Mason Ballowe · May 10, 2015 · Viewed 18.5k times · Source

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.

Answer

Mason Ballowe picture Mason Ballowe · May 10, 2015

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.