小编典典

UITableViewCell多选时选择的背景色

swift

// 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 cellBGView = UIView()
cellBGView.backgroundColor = UIColor(red: 0, green: 0, blue: 200, alpha: 0.4)
cell.selectedBackgroundView = cellBGView

任何答案如何设置所选单元格的背景色?


阅读 263

收藏
2020-07-07

共1个答案

小编典典

斯威夫特4.2

对于多个选择,您需要将UITableView属性设置allowsMultipleSelectiontrue

myTableView.allowsMultipleSelection = true

如果您将 UITableViewCell 子类化,则可以setSelected(_ selected: Bool, animated: Bool)在自定义单元格类中重写方法。

 override func setSelected(_ selected: Bool, animated: Bool) {
     super.setSelected(selected, animated: animated)

     if selected {
         contentView.backgroundColor = UIColor.green
     } else {
         contentView.backgroundColor = UIColor.blue
     }
 }
2020-07-07