将子图层添加到UICollectionViewCell时出现意外行为

时间:2015-03-29 16:09:38

标签: ios swift uicollectionview calayer

在以下最小示例中,我创建了一个UICollectionView,其中包含五个UICollectionViewCell个。对于每个人,我使用相同的CALayer创建frame并设置其backgroundColor属性,并将其作为子图层添加到UICollectionViewCell layer属性。最初在屏幕上的单元格按预期设置,但剩余的单元格可能是错误的颜色,并且在滚动时可能在完全脱离屏幕之前消失。这个问题[1]表明这种情况正在发生,因为细胞最初不在屏幕上(?),但我从答案中看不到如何解决问题。

以下是一个最小的工作示例。我尝试过的一些事情被评论出来了:

  1. 直接设置单元格的背景颜色,以确保这不是我设置集合视图的方式的问题(它不是)。
  2. 致电setNeedsDisplay()(无效)。
  3. 删除单元格图层的子图层(滚动时崩溃)。
  4. import Foundation
    import UIKit
    
    enum Colors: Int {
        case Red
        case Orange
        case Yellow
        case Green
        case Blue
    }
    
    class TestViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    
        var collectionView = UICollectionView(frame: CGRect(x: 100, y: 100, width: 100, height: 100), collectionViewLayout: UICollectionViewFlowLayout())
        let reuseIdentifier = "ColorCell"
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.collectionView.dataSource = self
            self.collectionView.delegate = self
            self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "ColorCell")
            self.view.addSubview(self.collectionView)
        }
    
        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 5
        }
    
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
            let cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
    
            var l = CALayer()
            l.frame = cell.frame
            l.delegate = self
    
            if let color = Colors(rawValue: indexPath.item) {
                switch color {
                case .Red:
                    l.backgroundColor = UIColor.redColor().CGColor
    //                cell.backgroundColor = UIColor.redColor()
                case .Orange:
                    l.backgroundColor = UIColor.orangeColor().CGColor
    //                cell.backgroundColor = UIColor.orangeColor()
                case .Yellow:
                    l.backgroundColor = UIColor.yellowColor().CGColor
    //                cell.backgroundColor = UIColor.yellowColor()
                case .Green:
                    l.backgroundColor = UIColor.greenColor().CGColor
    //                cell.backgroundColor = UIColor.greenColor()
                case .Blue:
                    l.backgroundColor = UIColor.blueColor().CGColor
    //                cell.backgroundColor = UIColor.blueColor()
                }
            } else {
                l.backgroundColor = UIColor.blackColor().CGColor
    //            cell.backgroundColor = UIColor.redColor()
            }
    
    //        for sub in cell.layer.sublayers {
    //            sub.removeFromSuperlayer()
    //        }
            cell.layer.addSublayer(l)
    //        cell.setNeedsDisplay()
    
            return cell
    
        }
    
    }
    

    [1] CALayer delegate method drawLayer not getting called

1 个答案:

答案 0 :(得分:1)

主要问题是你应该将图层的框架设置为单元格的边界,而不是其框架。但是,另一个问题是,当您滚动并重复使用单元格时,您将添加其他图层,因为每次调用cellForItemAtIndexPath时都会添加子图层。为了解决这些问题,我将创建一个UICollectionViewCell的子类,并在那里添加和调整图层,

class CustomCollectionViewCell: UICollectionViewCell {

    var l = CALayer()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        l.frame = self.bounds
        layer.addSublayer(l)
    }
}

然后,cellForItemAtIndexPath成为,

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifier, forIndexPath: indexPath) as CustomCollectionViewCell

        cell.l.delegate = self

        if let color = Colors(rawValue: indexPath.item) {
            switch color {
            case .Red:
                cell.l.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.5).CGColor
            case .Orange:
                cell.l.backgroundColor = UIColor.orangeColor().CGColor
            case .Yellow:
                cell.l.backgroundColor = UIColor.yellowColor().CGColor
            case .Green:
                cell.l.backgroundColor = UIColor.greenColor().CGColor
            case .Blue:
                cell.l.backgroundColor = UIColor.blueColor().CGColor
            }
        } else {
            cell.l.backgroundColor = UIColor.blackColor().CGColor
        }
        return cell
    }

如果这样做,请务必在viewDidLoad中注册自定义类而不是UICollectionViewCell。