1

Is there a way to color every other cell one color on a tableView? I tried looking into the properties for my tableView but were not able to find anything.

I want it like this:

tableview
 cell - white
 cell - gray
 cell - white
 cell - gray

etc...

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • Possible duplicate of [Color alternate UITableViewCell in a UITableView?](https://stackoverflow.com/questions/30505330/color-alternate-uitableviewcell-in-a-uitableview) – ItsMeMihir Jun 17 '17 at 12:51

2 Answers2

8

One line code, if you want

 cell.backgroundColor = indexPath.row % 2 == 0 ? .white : .gray
hardikdevios
  • 1,779
  • 14
  • 33
2

The easiest way will be to do this programmatically in your cellForRowAt function:

if indexPath.row % 2 == 0 {
    cell.backgroundColor = UIColor.white
} else {
    cell.backgroundColor = UIColor.gray
}
Rashwan L
  • 38,237
  • 7
  • 103
  • 107