UITableViewCell checkmark to be toggled on and off when tapped

前端 未结 14 1477
执念已碎
执念已碎 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:56

    A UITableView keeps selected state for single or multiple selections. So IMO there would need to be a very good reason for keeping an entire parallel state somewhere. If you want to just change the cell's appearance based on select state, do it in the cell.

    In your UITableViewCell subclass, override setSelected like so:

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
    }
    

    No need to use any table view delegate methods.

    Note: You have to call super.setSelected otherwise the cell doesn't keep the selected state correctly.

提交回复
热议问题