【问题标题】:UICollectionViewDataSource cellForItemAt don't run at iOS 14UICollectionViewDataSource cellForItemAt 不在 iOS 14 上运行
【发布时间】:2021-04-13 18:27:48
【问题描述】:

在 Xcode 中将构建目标更新为 iOS 14 后,不会调用 cellForItemAt。但是, numberOfItemsInSection 被调用,但 cellForItemAt 却没有,这很奇怪。我以前从未见过。

知道它可能是什么吗?

extension LoginTableCell: UICollectionViewDelegate, UICollectionViewDataSource {

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LoginCollectionCell", 
    return cell
}

【问题讨论】:

  • 首先检查 self.cells 中元素的数量。这通常是问题所在:一个空数组

标签: swift uicollectionview ios14


【解决方案1】:

我看不到您设置单元格大小的任何地方,因此请确保您的单元格内容大小设置正确。如果您使用的是流式布局,您可以像这样设置大小:

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: 50, height: 50)
collectionView.collectionViewLayout = layout

您也可以使用 UICollectionViewDelegateFlowLayout 协议来返回单元格的大小:

extension LoginTableCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LoginCollectionCell", 
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 100, height: 100)
}

【讨论】:

  • 就是这样,自从更改为iOS14以来,单元格已经失去了它的布局。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
相关资源
最近更新 更多