集合查看单元格留空顶部

时间:2017-12-20 14:03:21

标签: ios uiscrollview uicollectionview uicollectionviewdelegateflowlayout

我在垂直滚动模式下使用具有不同单元大小的集合视图。我希望单元格显示如图所示。

Required Image

但是集合视图单元格看起来像这样,顶部有额外的空间。

Image from my project

以下是项目代码:

 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,但没有任何反复发生。

1 个答案:

答案 0 :(得分:0)

在我看来,您使用的是典型的流程布局,它会创建行中最高项目的高度,并垂直居中该行中的所有项目。我的猜测是细胞不占用所有空间(可能是因为自动布局和图像尺寸?)。

无论原因是什么,看起来你正试图尽可能地从上到下定位细胞,忽略行。

我最近通过跟随Ray Wenderlich的this guide非常成功地完成了其中一项

相关问题