Select multiple rows in tableview and tick the selected ones

前端 未结 5 1681
北海茫月
北海茫月 2020-12-12 17:34

I\'m loading a tableView from a plist file. This works with no problems. I just simply want to \"tick\" the selected rows. At the moment, with my code it didn\'t work as des

5条回答
  •  情话喂你
    2020-12-12 18:14

    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 :

提交回复
热议问题