When I select on a cell on my table view, it changes into this white color. I want to change it.
I tried using an if statement but it didn't work.
Here is the code that I used.
override func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
if cell.isSelected == true {
cell.backgroundColor = .blue
} else {
cell.backgroundColor = .red
}
return cell
}
You can do either one of this -
For example: If you change it to UITableViewCellSelectionStyleGray, it will be gray.
cell.selectionStyle = UITableViewCellSelectionStyleGray;
Change the selectedBackgroundView property.
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.red
cell.selectedBackgroundView = bgColorView
swift 4 correction:
cell.selectionStyle = UITableViewCellSelectionStyle.gray
Maybe you need this:
cell.selectedBackgroundView
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With