CollectionViewCell 没有在 运行 的第一次出现

CollectionViewCell didn't appear at the first time of running

我在 Swift 中使用 UICollectionView 时遇到了这个奇怪的错误,我不知道它是从哪里来的。

所以我的 collection 视图有一个正常设置:

func setupCollectionView() {
    ColorTagViewCell.registerCellByNib(self.colorTagCollection)
    self.colorTagCollection.delegate = self
    self.colorTagCollection.dataSource = self
}

函数委托的实现:

extension EventViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 20, height: 20)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return colorStringList.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = ColorTagViewCell.loadCell(self.colorTagCollection, path: indexPath) as? ColorTagViewCell else {return UICollectionViewCell()}
        cell.setupViewForCell(colorString: colorStringList[indexPath.row], state: colorSelection[indexPath.row])
        return cell
    }
}

当我 运行 应用程序时 collection 视图单元格没有出现,尽管我已经为每个单元格设置了默认背景颜色

但是当我点击任何单元格时,它会出现并且看起来像这样:

我还没有实现任何单元格交互方法或点击之类的东西。我不知道这个错误是什么,我已经尝试修复它一天了,但没有任何运气。 我还在 viewDidLoad() 中调用 reloadData() 并调试 UI 并注意到我的 collection 视图仍然存在,只是没有显示颜色。

我的 ColorTagViewCell class,我在其中格式化和设置单元格:

class ColorTagViewCell: BaseCLCell {
    @IBOutlet weak var outerView: UIView!
    @IBOutlet weak var innerView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func layoutSubviews() {
        self.roundedView(views: [self.outerView, self.innerView])
    }

    func roundedView(views: [UIView]) {
        views.forEach { (view) in
            view.layer.cornerRadius = view.frame.size.width / 2
            view.layer.masksToBounds = true
        }
    }

    func setupViewForCell(colorString: String, state: Bool) {
        self.outerView.backgroundColor = UIColor.colorFromHexString(hex: colorString)
        self.innerView.isHidden = state
    }
}

我已经解决了我的问题,错误是由 ColorTagViewCell 引起的 class

这是我为修复错误而更新的代码:

class ColorTagViewCell: BaseCLCell {
    @IBOutlet weak var outerView: UIView!
    @IBOutlet weak var innerView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    func roundedView(views: [UIView]) {
        views.forEach { (view) in
            view.layer.cornerRadius = view.frame.size.width / 2
            view.layer.masksToBounds = true
        }
    }

    func setupViewForCell(colorString: String, state: Bool) {
        self.outerView.backgroundColor = UIColor.colorFromHexString(hex: colorString)
        self.innerView.isHidden = state
        self.layoutIfNeeded()
        self.roundedView(views: [self.outerView, self.innerView])
    }
}

我只是添加了“layoutIfNeeded()”函数,它解决了问题。正如我的代码所示,我想通过使用我的委托函数 cellForItemAt() 和方法 setupViewForCell() 来填充单元格背景颜色,同时我想用 roundedView() 方法将其图层舍入,所以我认为它不知道首先要做什么,因为视图是异步呈现的。为我的单元格设置背景颜色后,我使用“layoutIfNeeded()”函数然后我将单元格四舍五入,现在它工作得很好。