Frank Minyon Frank Minyon -4 years ago 193
Swift Question

Swift 3 - Cannot change background color on my Cell.AccessoryView

I'm trying to get the background of my UITableViewCell's AccessoryView to be the same color as the rest of my cell, but I cannot get it work out. No matter what the AccessoryView background is always gray

My UITableView uses STATIC cells, so no data is loaded.

Here's my code:

// Table setup

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .checkmark
cell?.contentView.backgroundColor = #colorLiteral(red: 0.2250251152, green: 1, blue: 0.1816247099, alpha: 0.5082940925)
cell?.backgroundColor = #colorLiteral(red: 0.2250251152, green: 1, blue: 0.1816247099, alpha: 0.5082940925)
(superView as! ProjectTestSelectViewController).updateProjectHandlerTests(cell!, didSelect: true)
}

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.contentView.backgroundColor = UIColor(white: 1, alpha: 0.2)
cell?.backgroundColor = UIColor(white: 1, alpha: 0.2)
cell?.accessoryType = .none
(superView as! ProjectTestSelectViewController).updateProjectHandlerTests(cell!, didSelect: false)
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.contentView.backgroundColor = UIColor(white: 1, alpha: 0.2)
cell.backgroundColor = UIColor(white: 1, alpha: 0.2)
}

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor(white: 1, alpha: 0.8)
}


Here's a screenshot

enter image description here

I've tried using the code below, but it does not effect the background color.


cell?.accessoryView?.backgroundColor = UIColor.clear

Answer Source

After our discussion, it's best for you to implement standard UITableView methods to achieve your goal and set selectionStyle to .none. Here is a link to a tutorial, to help you with implementing the methods.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download