我在垂直滚动模式下使用具有不同单元格大小的集合视图。我希望单元格显示为如图所示。
但是集合视图单元格看起来像这样,顶部有额外的空间。
以下是项目的代码:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return self.allProducts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "demoItem", for: indexPath) as! ItemCollectionViewCell
let item = allProducts[indexPath.row]
cell.lbl_name.text = item.title
cell.lbl_price.text = "$ " + item.price
cell.imgView.image = imgArray[indexPath.row % imgArray.count]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.row % 3 == 0
{
return CGSize(width: collectionView.frame.size.width/2, height: 200)
}
else if indexPath.row % 2 == 0
{
return CGSize(width: collectionView.frame.size.width/2, height: 225)
}
return CGSize(width: collectionView.frame.size.width/2, height: 250)
}
如何修复单元格上方的空间问题。
注意:我尝试将 automaticEdgeInset 设置为 false 并手动更改 contentInsets 但没有任何反应。