Does anyone know how to change the background color of a cell using UITableViewCell, for each selected cell? I created this UITableViewCell inside the code for TableView.
SWIFT 5.X
It also works when accessoryType changed for cell
extension UITableViewCell{
var selectedBackgroundColor: UIColor?{
set{
let customColorView = UIView()
customColorView.backgroundColor = newValue
selectedBackgroundView = customColorView
}
get{
return selectedBackgroundView?.backgroundColor
}
}
}
And in UIViewController use like below...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! myCell
cell.selectedBackgroundColor = UIColor.lightGray
return cell
}