Swift 4
First, make your tableView support multiple selection :
self.tableView.allowsMultipleSelection = true
self.tableView.allowsMultipleSelectionDuringEditing = true
Then simply subclass UITableViewCell like this :
class CheckableTableViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.accessoryType = selected ? .checkmark : .none
}
}
Finally, use it in your cellForRowAt indexPath
as such :
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
for: indexPath) as? CheckableTableViewCell
If you have to, don't forget to subclass your prototype cell in your xib/storyboard :
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…