In my tableView I set a separator line between cells. I am allowing selection of multiple cells. Here\'s my code for setting selected cell background color:
I needed the following:
"When user selects row, selection background color is transparent/white/whatever you may call it and separator lines don't disappear"
I've looked as well for a solution for the following problem:
"When I select a row in a table (plain type table) I had selection colour grey, and if I set cell.selectionStyle to none -> Separators between cells disappeared."
Xcode - 9.2 version
Found the following solution:
let colorView = UIView(frame: CGRect(x: 0.0, y: 3.0, width:
cell.frame.width, height: cell.frame.height - 1.0))
colorView.backgroundColor = UIColor.white
UITableViewCellClass.appearance().selectedBackgroundView = colorView
UITableViewCellClass - is your prototype cell class it makes possible to change selection color to white
in 'tableView (...didSelectRowAt)'
cell.selectionStyle = .none
in UITableViewCellClass (your prototype cell class)
override func layoutSubviews() { super.layoutSubviews()
subviews.forEach { (view) in
if type(of: view).description() == "_UITableViewCellSeparatorView" {
view.alpha = 1.0
}
}
}
it allows to keep selected row with check mark and all separators are in place.