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
}
I had a recent issue with an update to Swift 5 where the table view would flash select and then deselect the selected cell. I tried several of the solutions here and none worked. The solution is setting clearsSelectionOnViewWillAppear
to false.
I had previously used the UIView and selectedBackgroundColor property, so I kept with that approach.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "popoverCell", for: indexPath) as! PopoverCell
let backgroundView = UIView()
backgroundView.backgroundColor = Color.Blue
cell.selectedBackgroundView = backgroundView
}
Below are the changes I needed for Swift 5. The property clearsSelectionOnViewWillAppear
was the reason my cells were deselecting. The following select was necessary on first load.
override func viewDidLoad() {
super.viewDidLoad()
clearsSelectionOnViewWillAppear = false
popoverTableView.selectRow(at: selectedIndexPath, animated: false, scrollPosition: .none)
}
After some testing this WILL remove the background color when deselected or cell is tapped a second time when table view Selection is set to "Multiple Selection". Also works when table view 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 you see below, your cell's Selection property can be set to anything BUT None.
Style: Plain, Selection: Single Selection
Style: Plain, Selection: Multiple Selection
Style: Grouped, Selection: Multiple Selection
For a smoother color transition, try some 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
})
}
}
}
You may notice the icon and text color also changing when cell is selected. This happens automatically when you set the UIImage and UILabel Highlighted properties
UIImage
UILabel
Just supply a color for the Highlighted property:
var last_selected:IndexPath!
define last_selected:IndexPath inside the class
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! Cell
cell.contentView.backgroundColor = UIColor.lightGray
cell.txt.textColor = UIColor.red
if(last_selected != nil){
//deselect
let deselect_cell = tableView.cellForRow(at: last_selected) as! Cell
deselect_cell.contentView.backgroundColor = UIColor.white
deselect_cell.txt.textColor = UIColor.black
}
last_selected = indexPath
}
// animate between regular and selected state
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
self.backgroundColor = [UIColor colorWithRed:234.0f/255 green:202.0f/255 blue:255.0f/255 alpha:1.0f];
}
else {
self.backgroundColor = [UIColor clearColor];
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor yellowColor];
}