微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 如何更改每个uitableviewcell背景颜色

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

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

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

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])
    }

您需要删除此行
let cell = tableView.dequeueReusableCellWithIdentifier("cell",forIndexPath: indexPath) as! GroupTableViewCell

来自你的willdisplayCell函数,因为它已经将你的单元格放在参数中,而你只是用新单元格覆盖它,而你的新单元格将永远不会被使用.

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

var cellColors = ["F28044",forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor(hexString: cellColors[indexPath.row % cellColors.count])
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐