user8155701
user8155701

Reputation:

TableView every other cell one color

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...

Upvotes: 1

Views: 877

Answers (2)

hardikdevios
hardikdevios

Reputation: 1789

One line code, if you want

 cell.backgroundColor = indexPath.row % 2 == 0 ? .white : .gray

Upvotes: 8

Rashwan L
Rashwan L

Reputation: 38833

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
}

Upvotes: 2

Related Questions