I am trying to change the cell's background color depending on their placement, i would like to create an effect like this
This is the code i use
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if totalCells % 2 == 0 {
cell.backgroundColor = UIColor(red: 200/255.0, green: 200/255.0, blue: 200/255.0, alpha: 1.0)
} else {
cell.backgroundColor = UIColor.white
}
}
The problem is that the cells are in a scrolling tableview, and that when I scroll, the cells get reused for memory purposes, and the colors start changing incorrectly, like the same colors are ending up next to each other, which is not supposed to happen.
I am updating the totalCells Variable whenever I add a cell (inside the cellForRowAt function)
How could I fix this?
Thank you!