Swift UICollectionViewDataSource cellForItemAt don';我不能在iOS 14上运行

Swift UICollectionViewDataSource cellForItemAt don';我不能在iOS 14上运行,swift,uicollectionview,ios14,Swift,Uicollectionview,Ios14,在我在Xcode中将构建目标更新为iOS 14后,cellForItemAt没有被调用。然而,numberOfItemsInSection正在被调用,但cellForItemAt没有被调用,这使得它很奇怪。我以前从未见过这种情况 知道会是什么吗 extension LoginTableCell: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UIColle

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

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

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

首先检查self.cells中的元素数。这通常是问题的根源:一个空的数组。如果是这样的话,单元格在更改为iOS14后就失去了布局。非常感谢。