ios - Swift:CollectionView 空间

标签 ios swift

我的 CollectionView 中有 12 个单元格。我想在一个屏幕上显示 3 个单元格,如下所示:

enter image description here

并向右滚动以显示另一个单元格。

但现在我创建 CollectionViewCollectionViewCell 并在 storyboard 中编辑单元格,得到以下结果:

enter image description here

如何解决?

我在CollectionView中的代码:

import UIKit

private let reuseIdentifier = "Cell"

class MasterViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {

        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 12
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MasterViewCell

        cell.label.text = String(indexPath.row)
        cell.backgroundColor = UIColor.green

        return cell
    }
}

我在CollectionViewCell中的代码:

import UIKit

class MasterViewCell: UICollectionViewCell {

    @IBOutlet weak var label: UILabel!
}

最佳答案

要实现该间距,您需要这样做

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 4
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

因此,在新部分中的每 3 个单元格之后,它将采用该部分插入并提供该空间。

enter image description here

关于ios - Swift:CollectionView 空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50973528/

相关文章:

objective-c - 从存档文件恢复对象

ios - 获取在 VNDetectTextRectanglesRequest 完成处理程序上的 VNImageRequestHandler 中使用的 cvPixelBuffer

ios - 蓝牙低功耗作为 iOS 设备要求?

swift - 正确转换 UICollectionViewCell 中的 subview

arrays - 排序数组的问题

ios - 在不首先获取对象的情况下更新 Parse 上的值(如 SQL 更新)

ios - 如何改善我的ios App中图像的移动

ios - 我如何自动更新 bool 变量的值

ios - 使用 Swift4 从 URL 加载图像并将其存储在本地

swift - 从 AVCapturePhotoDelegate 方法检索 CVPixelBuffer