3

I have a dynamic tableView with 2 prototype cells. I am using one of the cells for section header, the section header cell has it's own class. Data have been populated to these cells without problems.

I am getting this error message "Error: “Unexpected nil index path in _canPerformAction:forCell:sender:, this should never happen.” at runtime when I tap on the section header. Anyone knows how to get rid of this error? Thanks in advance!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {  
    let cell = tableView.dequeueReusableCell(withIdentifier: "MachineTableViewCell") as! MachineTableViewCell  

    if self.uptime.count == self.machines.count {  
        cell.GPUNumber.text = self.allGPUNumber[indexPath.section][indexPath.row]  
    }  

    return cell  
}  

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {  
    guard let header = tableView.dequeueReusableCell(withIdentifier: "header") as? HeaderTableViewCell  
        else {  
            return nil  
        }  

    let machine = machine[section]  
    header.name.text = machine.name + " - " + machine.ip + ":" + machine.port  
    return header  
}  

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat  
{  
    return 124  
}
Kevin W
  • 31
  • 4

1 Answers1

2

I initially missed this as well but if you take a step back and look at the return type you almost kick yourself.

For a regular cell the return type is UITableViewCell.

However for the header it's UIView.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {  
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")...
    ///
    return cell  
}  

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {  
   let header = tableView.dequeueReusableCell(withIdentifier: "header")...
   ///
   return header  
}

Therefore, what you need to do is return the contentView.

return header.contentView
dschlabach
  • 81
  • 4