UITableViewCell checkmark to be toggled on and off when tapped

前端 未结 14 1445
执念已碎
执念已碎 2020-11-30 22:25

I\'m working on a tableview

I want to be able to tap on each cell and when tapped, it displays a checkmark on the cell

Now I have some code that makes this w

14条回答
  •  失恋的感觉
    2020-11-30 22:52

    The simplest solution that did it for me (Swift 5.2)

    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        // Remove checkmark from the row that is currently showing it before adding to one being selected
        if let currentIndexPath = tableView.indexPathForSelectedRow {
            self.tableView.cellForRow(at: currentIndexPath)?.accessoryType = .none
        }
    
        return indexPath
    }
    
    override public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    }
    

提交回复
热议问题