tableview单元格在重用后有错误的背景图层

时间:2018-01-30 23:50:45

标签: ios swift uitableview

我有一个UIView扩展,它使用两种颜色制作渐变背景。我使用这个,所以我添加了一个很好的背景到我的自定义tableView单元格。但是在重用之后,颜色总是错误的(不像里面的数据是正确的)。它不像普通的背景颜色,所有颜色都取决于获取数据的值。重用后,背景始终是先前生成的单元格(及其背景)的随机。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell

    var imgTitle = ""
    var color = UIColor()
    let title = fetchedUV[indexPath.row].value
    if title >  11 {
        imgTitle = "flame"
        color = UIColor.purple
        cell.setGradientBackground(colorOne: .white, colorTwo: color)
    } else if title >= 8 {
        imgTitle = "sun-protection"
        color = UIColor.red
        cell.setGradientBackground(colorOne: .white, colorTwo: color)
    } else if title >= 6 {
        imgTitle = "sunbed"
        color = UIColor.orange
        cell.setGradientBackground(colorOne: .white, colorTwo: color)
    } else if title >= 3 {
        imgTitle = "sunglasses"
        color = UIColor.yellow
        cell.setGradientBackground(colorOne: .white, colorTwo: color)
    } else {
        imgTitle = "ok"
        color = UIColor.green
        cell.setGradientBackground(colorOne: .white, colorTwo: color)
    }

    colors.append(color)
    let UVValue = String(describing: title)

    cell.backgroundColor = UIColor.orange
    cell.commonInit(imgTitle, title: UVValue, time: fetchedUV[indexPath.row].dateIso)

    cell.logoImage.layer.cornerRadius = (cell.frame.height) / 2
    cell.layer.cornerRadius = cell.frame.height/2


    cell.layer.masksToBounds = true
    //cell.setGradientBackground(colorOne: .white, colorTwo: color)

    return cell
}

extension UIView {
    func setGradientBackground(colorOne: UIColor, colorTwo: UIColor) {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]
        gradientLayer.locations = [0.0, 1.0]
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
        layer.insertSublayer(gradientLayer, at: 0)
    }
}

2 个答案:

答案 0 :(得分:1)

仅举例来看看这个

extension UIView
{
    //This func will add gradient backgroung
    //Just sample one
    func setGradientBackground()
    {
        //Colors
        let colorTop =  UIColor(red: 255.0/255.0, green: 149.0/255.0, blue: 0.0/255.0, alpha: 1.0).cgColor
        let colorBottom = UIColor(red: 255.0/255.0, green: 94.0/255.0, blue: 58.0/255.0, alpha: 1.0).cgColor

        //Set Gradient layer
        let gradientLayer = CAGradientLayer()

        //Colors
        gradientLayer.colors = [ colorTop, colorBottom]

        //Locations
        gradientLayer.locations = [ 0.0, 1.0]

        //Here is the main Play
        //Set Layer name so can be identified while Dequeuing cell
        gradientLayer.name = "layerName"

        //Set bounds
        gradientLayer.frame = self.layer.bounds

        //Insert Layer
        self.layer.addSublayer(gradientLayer)
    }
}

现在在TableView的CellForRowAt中

//Setting cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        //Get all the sublayers of cell
        for sublayer in cell.layer.sublayers!
        {
            //Check that sublayer is Already Added or not
            if sublayer.name == "layerName"
            {
                //Sublayer already added
                //Print already Added
                print("Cell Deque again")
            }
            else
            {
                //Sublayer is not added yet
                //Time to add Gradient Background
                cell.setGradientBackground()
                print("Layer Added")
            }
        }

        //setting title
        cell.textLabel?.text = items[indexPath.section][indexPath.row]

        return cell
    }

希望它有所帮助,你可以命名一个被添加为SubLayer的图层,并将其添加到Cell时,因为它一次又一次地获取Deque,所以你必须看到该图层不会覆盖先前添加的图层

答案 1 :(得分:1)

我找到并删除了之前添加的图层(CAGradientLayer,因为它也可以删除任何其他图层)。如果需要,执行检查并添加另一个图层。我还更改了函数,因此图层现在有一个名称。 (感谢iOS Geek的建议)

for sublayer in cell.layer.sublayers! {
                if let _ = sublayer as? CAGradientLayer {
                    if sublayer.name == name {
                        print("Cell deque again")
                    } else {
                        sublayer.removeFromSuperlayer()
                        cell.setGradientBackground0(colorOne: .white, colorTwo: color, name: name)
                    }
                } else {
                    cell.setGradientBackground0(colorOne: .white, colorTwo: color, name: name)
            }
        }

extension UIView {
func setGradientBackground0(colorOne: UIColor, colorTwo: UIColor, name: String) {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]
        gradientLayer.locations = [0.0, 1.0]
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
        gradientLayer.name = name
        layer.insertSublayer(gradientLayer, at: 0)
    }
}

我会留下这个以防万一。