TableView每隔一个单元格一种颜色

时间:2017-06-17 12:11:18

标签: swift colors tableview

有没有办法在tableView上为每个其他单元格着色一种颜色?我试着查看tableView的属性,但无法找到任何内容。

我想这样:

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

等...

2 个答案:

答案 0 :(得分:2)

一行代码,如果您需要

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

答案 1 :(得分:0)

最简单的方法是在cellForRowAt函数中以编程方式执行此操作:

if indexPath.row % 2 == 0 {
    cell.backgroundColor = UIColor.white
} else {
    cell.backgroundColor = UIColor.gray
}
相关问题