didDeselectRowAtIndexPath indexPath is nil

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 14:17:22

问题


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.

来源:https://stackoverflow.com/questions/36875083/diddeselectrowatindexpath-indexpath-is-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!