我的 CollectionView
中有 12 个单元格。我想在一个屏幕上显示 3 个单元格,如下所示:
并向右滚动以显示另一个单元格。
但现在我创建 CollectionView
和 CollectionViewCell
并在 storyboard
中编辑单元格,得到以下结果:
如何解决?
我在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 个单元格之后,它将采用该部分插入并提供该空间。
关于ios - Swift:CollectionView 空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50973528/