Select multiple rows in tableview and tick the selected ones

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 :
enter image description here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)