Swift – UITableViewCellselect多个select的背景颜色

//Don't Works... cell.selectionStyle = .Blue //Works when the selection is not multiple, if it's multiple with each selection the previos one disappear... let cellBGView = UIView() cellBGView.backgroundColor = UIColor(red: 0, green: 0, blue: 200, alpha: 0.4) cell.selectedBackgroundView = cellBGView 

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

这对我工作:

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! selectedCell.contentView.backgroundColor = UIColor.redColor() } // if tableView is set in attribute inspector with selection to multiple Selection it should work. // Just set it back in deselect override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { var cellToDeSelect:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! cellToDeSelect.contentView.backgroundColor = colorForCellUnselected } //colorForCellUnselected is just a var in my class 

所有上述的答案都很好,但有点复杂,我喜欢。 最简单的方法是将一些代码放在cellForRowAtIndexPath 。 这样,您不必担心在取消单元格时更改颜色。

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) /* this is where the magic happens, create a UIView and set its backgroundColor to what ever color you like then set the cell's selectedBackgroundView to your created View */ let backgroundView = UIView() backgroundView.backgroundColor = YOUR_COLOR_HERE cell.selectedBackgroundView = backgroundView return cell } 

Swift 3

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath) cell.selectionStyle = .none return cell } 

Swift 2

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath) cell.selectionStyle = .None return cell } 

克尔斯诺夫斯基的方法的问题是,当单元格被重新绘制时,select/取消select所做的更改将消失。 所以我会把变化移到单元格本身,这意味着子类化在这里是必需的。 例如:

 class ICComplaintCategoryCell: UITableViewCell { @IBOutlet var label_title: UILabel! @IBOutlet var label_checkmark: UILabel! override func layoutSubviews() { super.layoutSubviews() reload() } func reload() { if selected { contentView.backgroundColor = .redColor() label_checkmark.hidden = false } else { contentView.backgroundColor = .whiteColor() label_checkmark.hidden = true } } } 

而在你的表视图委托只是调用reload

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

您也可以在界面生成器中将单元格的selectionStyle设置为.none 。 与@AhmedLotfy提供的解决scheme相同,只能从IB获得。

在这里输入图像说明

Swift 3

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)! selectedCell.contentView.backgroundColor = UIColor.darkGray } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)! selectedCell.contentView.backgroundColor = UIColor.clear }