didDeselectRowAtIndexPath indexPath is nil

2019-03-06 09:32发布

问题:

I have a TableViewController which implements the TableViewDelegate methods:

public override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! ParticipantesSearchTableViewCell
    cell.viewModel?.active = true
}

public override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! ParticipantesSearchTableViewCell
    cell.selectedStateImageView.hidden = true
    cell.viewModel?.active = false
}

Please note that I never remove nor set my tableView.editing to true. Also, my numberOfRowsInSection method never changes.

If I set my tableView to tableView.allowsSingleSelection = true I can select and deselect cells just fine and the above methods fire as aspected. However, when I scroll on my tableView and select a new cell the method didDeselectRowAtIndexPath crashes on let cell = tableView.cellForRowAtIndexPath(indexPath) as! ParticipantesSearchTableViewCell

At this point I debugged tableView.indexPathForSelectedRow and it returns nil.

Any idea what I'm implementing wrong here or if this is a bug? Because I try to use the same table with tableView.allowsMultipleSelection = true and I can select, deselect, and scroll just fine.