集合视图单元格正在相互混合

CollectionViewCells are being mixed with each other

各位溢出者大家好,所以我设置了一个 collectionView 并对其进行了配置,但是当我 运行 模拟器时,我得到了很多像第一张图片中那样相互混合的单元格。

我在情节提要单元格中添加了标识符,还为该单元格创建了 class,但我似乎不知道问题出在哪里,因为它从未发生过。

我希望我的手机是什么样子,我会在第二张图片上展示。

first picture

second picture

代码:

extension dashboardViewController: UICollectionViewDelegate, UICollectionViewDataSource {

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


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dashboardCollectionViewCell", for: indexPath) as? dashboardCollectionViewCell else {return UICollectionViewCell()}
    
    return cell
  }
}

您的集合视图单元格大小似乎设置不正确。 如果您正在使用 UICollectionViewFlowLayout,也许您可​​以检查布局对象的 itemSize 属性.

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

...

// Set a suitable CGSize for the `itemSize` property
// In this example I set it to 100pt * 50pt
layout.itemSize = CGSizeMake(100, 50);

...

UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];

根据提供的图片,您似乎没有设置单元格的项目大小 在水平布局中以及每个单元格之间的最小间距。您可以通过调用

来做到这一点(设置项目大小)
func collectionView(
  _ collectionView: UICollectionView,
  layout collectionViewLayout: UICollectionViewLayout,
  sizeForItemAt indexPath: IndexPath
) -> CGSize {}

此方法来自 UICollectionViewDelegateFlowLayout。 您也可以使用情节提要来执行此操作。 Size Inspector