如何更改每个uitableviewcell背景颜色

Mur*_*aya 6 uitableview uicolor swift

我想按顺序更改每个单元格背景颜色.

这是我的颜色.我只想在图像中显示它们.

我现在用随机颜色显示它.但我想按顺序显示它们.

var cellColors = ["F28044","F0A761","FEC362","F0BB4C","E3CB92","FEA375"]
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! GroupTableViewCell
        let randomColor = Int(arc4random_uniform(UInt32(self.cellColors.count)))
        cell.contentView.backgroundColor = UIColor(hexString: self.cellColors[randomColor])
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Sha*_* Of 7

您需要删除此行

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! GroupTableViewCell
Run Code Online (Sandbox Code Playgroud)

从你的willDisplayCell函数,因为它已经有你的单元格参数,你只是用新单元格覆盖它,你的新单元格将永远不会被使用.

如果要按顺序显示颜色,则可以使用indexPath:

var cellColors = ["F28044","F0A761","FEC362","F0BB4C","E3CB92","FEA375"]
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor(hexString: cellColors[indexPath.row % cellColors.count])
}
Run Code Online (Sandbox Code Playgroud)