UITableViewCell Selected Background Color on Multiple Selection

后端 未结 14 1047
夕颜
夕颜 2020-12-08 04:26
// Doesn\'t work
cell.selectionStyle = .Blue
//Works when the selection is not multiple, if it\'s multiple with each selection the previous one disappear...
let cell         


        
14条回答
  •  没有蜡笔的小新
    2020-12-08 04:48

    The problem with Kersnowski's approach is that when the cell is redrawn the changes made when it's selected/deselected will be gone. So I would move the changes into the cell itself, which means subclassing is required here. For example:

    class ICComplaintCategoryCell: UITableViewCell {
        @IBOutlet var label_title: UILabel!
        @IBOutlet var label_checkmark: UILabel!
    
        override func layoutSubviews() {
            super.layoutSubviews()
            reload()
        }
        func reload() {
            if isSelected {
                contentView.backgroundColor = UIColor.red
            }
            else if isHighlighted{
                contentView.backgroundColor = UIColor.red
            }
            else {
                contentView.backgroundColor = UIColor.white
            }
        }
    }
    

    And in your table view delegate just call reload:

    if let cell = self.table.cellForRowAtIndexPath(path) as? ICComplaintCategoryCell {
        cell.reload()
    }
    

    Updated for Swift 3+, thanks @Bogy

提交回复
热议问题