// 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
任何答案如何设置所选单元格的背景色?
斯威夫特4.2
对于多个选择,您需要将UITableView属性设置allowsMultipleSelection为 true 。
UITableView
allowsMultipleSelection
myTableView.allowsMultipleSelection = true
如果您将 UITableViewCell 子类化,则可以setSelected(_ selected: Bool, animated: Bool)在自定义单元格类中重写方法。
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 } }