增加集合视图中单元格之间间距的大小

时间:2018-04-12 13:58:38

标签: ios swift uitableview uicollectionview cell

我想在我的集合视图中的单元格之间留出不同的间距。

我的收藏夹只包含一个部分。我知道可以改变部分之间的间距,但我喜欢在细胞之间。

我希望中心单元格与其边之间的间距更大,并且其他单元格的间距不会移动。基本上,我的白色圆圈不会触及相邻单元格的边缘而不会改变所有间距。

我的问题图片在这里:

Image of my problem here

有解决方案吗?

2 个答案:

答案 0 :(得分:1)

您可以在视图控制器中实现此方法:collectionView:layout:minimumInteritemSpacingForSectionAtIndex

答案 1 :(得分:1)

您可以尝试创建3个不同的部分。

  1. 第1部分 - 包含较大单元格之前的单元格的部分,inter item spacing as 10
  2. 第二部分 - 包含较大单元格的部分,section insets as (top: 0, left: 20, bottom: 0, right: 20)
  3. 第3部分 - 包含较大单元格后的单元格的部分,inter item spacing as 10
  4. 您可以根据自己的要求更改值。

    以下是代码所说的内容:

    func numberOfSections(in collectionView: UICollectionView) -> Int
    {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        if section == 1
        {
            return 1
        }
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        if indexPath.section == 1
        {
            cell.backgroundColor = UIColor.red
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        if indexPath.section == 1
        {
            return CGSize(width: 100, height: 100)
        }
        return CGSize(width: 82, height: 82)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {
        if section == 1
        {
            return 0
        }
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        if section == 1
        {
            return UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        }
        return .zero
    }
    

    <强>截图:

    enter image description here

    如果您仍然遇到任何问题,请告诉我。