Reputation: 10738
I have a UITableView
that when a row is tapped a UIView
pops up, everything is working fine but what I want is to be able to change the background color of the row when it's tapped to other color than the default gray. The standard behavior which is how I have it right now, behaves as follow, when I tap the row it changes the background color to gray and when I dismiss the popup UIView
the background color changes back to the default white.
What I want is to be able to change the background color to blue when a row is tapped and changed it back to white when the UIView is dismissed.
I tried the following, which changes the color when the row is tapped but it doesn't change it back to white when the popup UIView is dismissed. In other words, it leaves the tapped rows with a blue background
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
/// Change color of the selected row
let selectedCell = tableView.cellForRow(at: indexPath)!
selectedCell.contentView.backgroundColor = UIColor.blue
}
How can I change the background of a cell when it's tapped and change it back to the default white when the popup UIView is dismissed?
FYI - I tried the didDeselectRowAt
but it's never called.
Thanks
Upvotes: 0
Views: 700
Reputation: 251
try this you have to set selection style to none when you return cell cell.selectionStyle = .none
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Your identifier", for: indexPath)
cell.selectionStyle = .none
return cell
}
Upvotes: 1
Reputation: 100503
didDeselectRowAt
is called when you tap another cell while the current is selected , you need to check viewDidAppear
if the dismiss calles it or use a delegate otherwise and inside it add this code
tableView.visibleCells.forEach {
$0.backgroundColor = .white
}
Upvotes: 2