我将如何以编程方式更改单元格的背景颜色

Jam*_*hen 2 uitableview uicolor ios swift

所以我的实际问题更加强大.我很好奇是否可以通过编程方式更改单元格的背景颜色,但这可能是基于单元格是第一个还是第二个等等.

我不确定这是否可能,但我想要实现的是利用细胞的梯度效应,因为它们的数量增加.

if (indexPath.row % 2)
    {
        cell.contentView.backgroundColor = UIColor.redColor()
    }
    else
    {
        cell.contentView.backgroundColor = UIColor.blueColor()
    }
Run Code Online (Sandbox Code Playgroud)

我尝试过这样的事情,至少让颜色交替出现,看看我能否弄清楚接下来要做什么,但这已经失败了.交替似乎不是正确的选择,但它可能是开始编程我想要发生的正确方法.

May*_*tel 7

A tableView在使用单元格绘制行之前将此消息发送到其委托,从而允许委托在显示单元格对象之前对其进行自定义.了解更多信息

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row % 2 == 0 {
         cell.backgroundColor = UIColor.redColor()
    }
    else {
         cell.backgroundColor = UIColor.blueColor()
    }
    return cell
}
Run Code Online (Sandbox Code Playgroud)