gpt4 book ai didi

ios - 为什么我的收藏 View 单元格不显示...?

转载 作者:行者123 更新时间:2023-11-28 10:40:13 25 4
gpt4 key购买 nike

自从我以编程方式创建 Collection View 以来已经有一段时间了,我似乎无法找到为什么我的单元格没有显示。正在设置委托(delegate)和数据源。尺寸正在设置中,不会太大。

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"

override func viewDidLoad() {
super.viewDidLoad()

setupCV()
}

var containerCollectionView: UICollectionView = {
let layout = UICollectionViewLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()
}

extension ViewController {
fileprivate func setupCV() {
// these first two lines add the colelction view and apply constraints - they work and the CV is properly displayed inside the view controller
self.view.addSubview(containerCollectionView)
containerCollectionView.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)

containerCollectionView.delegate = self
containerCollectionView.dataSource = self

containerCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 150, height: 150)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
cell.backgroundColor = UIColor.yellow
return cell
}

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

最佳答案

您正在初始化错误的布局。

试试这个:

let layout = UICollectionViewFlowLayout()

关于ios - 为什么我的收藏 View 单元格不显示...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50865945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com