Error: "Unexpected nil index path in _canPerformAction:forCell:sender:, this should never happen."

Error: "Unexpected nil index path in _canPerformAction:forCell:sender:, this should never happen."


I have a dynamic tableView with 2 prototype cells. I 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 at runtime, and 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
    }

Accepted Reply

You can do either of two pretty easy things:


1. Create a view XIB file for the header view, and instantiate the file to create new views.


2. Create a UIView subclass in code, and add subviews for the labels.


The "dequeue" mechanism is specifically for cells.

Replies

It's likely you can't use a dequeued UITableViewCell subclass for the header, because UITableViewCell is just going to have the behavior of responding to touches as if it's a regular cell (which is the apparent error you're getting). You can't use a non-UITableViewCell-subclass either, because the dequeue mechanism requires UITableViewCell subclasses.


You should use a freestanding view for the header, and implement your own re-use scheme if you have enough of them to matter.

Thanks for you reply QuinceyMorris.


Unfortunately I have to use dequeue mechanism for all the section headers, because they all have the same titles/labels with different data source. e.g, section one showing "computer#1", section two showing "computer#2"

You can do either of two pretty easy things:


1. Create a view XIB file for the header view, and instantiate the file to create new views.


2. Create a UIView subclass in code, and add subviews for the labels.


The "dequeue" mechanism is specifically for cells.