Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 快速交替UITableViewCell渐变颜色_Ios_Swift_Uitableview_Cagradientlayer - Fatal编程技术网

Ios 快速交替UITableViewCell渐变颜色

Ios 快速交替UITableViewCell渐变颜色,ios,swift,uitableview,cagradientlayer,Ios,Swift,Uitableview,Cagradientlayer,我以前在tableView willDisplay cell方法中有这个代码,但它不能准确地交替颜色-它几乎做到了,但有时还是会弄糟,1或2是相同的颜色,我不确定 我在自定义的UITableViewCell类中发现了一些建议调用awakeFromNib方法中的所有方法的内容,因此我将所有内容移动如下: class SectionTableViewCell: UITableViewCell { @IBOutlet weak var lblName: UILabel! @IBOutl

我以前在
tableView willDisplay cell
方法中有这个代码,但它不能准确地交替颜色-它几乎做到了,但有时还是会弄糟,1或2是相同的颜色,我不确定

我在自定义的
UITableViewCell
类中发现了一些建议调用
awakeFromNib
方法中的所有方法的内容,因此我将所有内容移动如下:

class SectionTableViewCell: UITableViewCell {
    @IBOutlet weak var lblName: UILabel!
    @IBOutlet weak var lblValue: UILabel!

    var cellGradient_1: CAGradientLayer = CAGradientLayer()
    var cellGradient_2: CAGradientLayer = CAGradientLayer()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.layer.insertSublayer(cellGradient_1, at: 0)
        self.layer.insertSublayer(cellGradient_2, at: 0)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        cellGradient_1.frame = self.bounds
        cellGradient_2.frame = self.bounds
    }

}
现在,在我的
表格视图cellForRowAt
中,我进行以下检查:

let cell = tableView.dequeueReusableCell(withIdentifier: "Section", for: indexPath) as! SectionTableViewCell
if (indexPath.row%2 == 0) {
    cell.cellGradient_1.colors = theme.childColor_1 //Some gradient color
}
else{
    cell.cellGradient_2.colors = theme.childColor_2 //Some gradient color
}
它们现在都是第一种颜色,底部附近的1是第二种颜色。我的
nib
有什么问题吗?有没有其他方法来设置渐变的颜色?任何帮助都将不胜感激


编辑:它看起来像是在初始加载时工作,但一旦重新加载表格或滚动发生,除2或3外,所有单元格的渐变都会更改为初始值。

使用两层,第一层覆盖第二层。
要解决此问题,只需删除第二层,并将
tableView cellForRowAt
中的代码更改为:

let cell = tableView.dequeueReusableCell(withIdentifier: "Section", for: indexPath) as! SectionTableViewCell
if (indexPath.row%2 == 0) {
    cell.cellGradient_1.colors = theme.childColor_1 //Some gradient color
}
else{
    cell.cellGradient_1.colors = theme.childColor_2 //Some gradient color
}

(我用cell.cellGradient_1替换了cell.cellGradient_2)

Ahhhhhh,现在我觉得有点傻。非常感谢您的收看!工作完美无瑕。