加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 02:26:26 所属栏目:百科 来源:网络整理
导读:我想按顺序更改每个单元格背景颜色. 这是我的颜色.我只想在图像中显示它们. 我现在用随机颜色显示它.但我想按顺序显示它们. var cellColors = ["F28044","F0A761","FEC362","F0BB4C","E3CB92","FEA375"]func tableView(tableView: UITableView,willDisplayCe
我想按顺序更改每个单元格背景颜色.

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读