【问题标题】:CollectionViewCell didn't appear at the first time of runningCollectionViewCell 在第一次运行时没有出现
【发布时间】:2021-04-05 22:20:10
【问题描述】:

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

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

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

当我运行应用程序时,虽然我为每个单元格设置了默认背景颜色,但集合视图单元格没有出现

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

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

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

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

【问题讨论】:

  • 你能说明你在哪里设置这些单元格的背景颜色,以及你通常在哪里设置它们的格式吗?也许他们在那里但看不见。
  • ibb.co/Zzx7PhV,这是我格式化和设置单元格的代码,不要介意 innerView 因为它是隐藏的,我之前评论过那行代码但它没有帮助,因为您可以看到我为它们中的每一个设置了背景颜色,但它只是在我运行应用程序时不出现,它只是在我单击它后出现。 ibb.co/xqY7QNc,当我捕获 UI 时的另一张图片,单元格确实有背景颜色,正如您在右侧面板中看到的那样,当它们没有出现时,这真的很奇怪,但它们仍然在视图中
  • edit您的问题以文本形式包含相关代码
  • 我刚刚编辑了我的问题以包含我的 CollectionViewCell 类的代码,我在其中格式化和设置单元格。还有什么我应该补充的来澄清我的问题吗?
  • 你能说明你在哪里设置使用的数组吗?另外,你在哪里调用 setupCollectionView()

标签: ios swift xcode uicollectionview


【解决方案1】:

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

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

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()”函数,然后将单元格四舍五入,现在它可以正常工作了。

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多