UITableView tap to deselect cell

前端 未结 14 1794
野性不改
野性不改 2020-12-16 10:37

I have a UITableViewCell that is selected when tapped. During this selected state, if the user taps the cell again, I want the cell to deselect.

I can\

14条回答
  •  醉话见心
    2020-12-16 11:31

    Leave the single selection mode on, but override the following funcs like this:

    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        if tableView.indexPathsForSelectedRows?.contains(indexPath) ?? false {
           tableView.deselectRow(at: indexPath, animated: true)
           return nil
        }
    
        return indexPath
    }
    
    override func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? {
        return nil
    }
    

提交回复
热议问题