SWIFT 4, XCODE 9, iOS 11
After some testing, this WILL remove the background color if you deselect or touch the cell a second time when the Selection table view is set to "Multiple Selection". Also works when the table presentation style is set to "Grouped".
extension ViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.contentView.backgroundColor = UIColor.darkGray } } }
Note. In order for this to work, as shown below, any BUT value can be set for the Cell Select property.
How it looks with different options
Style: Regular , Selection: Single Selection

Style: Normal , Selection: Multiple Choice

Style: grouped , selection: multiple choice

Bonus - Animation
For a smoother color transition, try the animation:
extension ViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { UIView.animate(withDuration: 0.3, animations: { cell.contentView.backgroundColor = UIColor.darkGray }) } } }

Bonus - change text and image
You may notice that the icon and text color also change when you select a cell. This happens automatically when you set the UIImage and UILabel Highlighted properties
UIImage
- Put two color images:

- Set the Highlighted Image property:

Uilabel
Just specify a color for the selected object:

Mark Moeykens Feb 28 '18 at 5:01 2018-02-28 05:01
source share