gpt4 book ai didi

ios - 'UICollectionView 必须使用非零布局参数进行初始化'

转载 作者:行者123 更新时间:2023-11-28 09:39:47 25 4
gpt4 key购买 nike

<分区>

在这 tutorial 之后,我第一次尝试创建一个简单的 collectionView

到目前为止,我已按照这些步骤进行操作,并确保我没有遗漏任何内容。问题是我收到此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

我查看了具有相同问题的不同帖子,但似乎没有一个答案可以修复我的代码。本教程没有为 collectionView 指定初始布局大小,所以我假设 Xcode 只是自动将其设置为 View 的边界,因此它没有非零布局?

class shootDaysCollectionView: UICollectionViewController, UICollectionViewDelegateFlowLayout {

private let cellId = "cellId"

override func viewDidLoad() {

}

// VIEW WILL APPEAR
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.title = "SHOOT DAYS"

collectionView?.backgroundColor = UIColor.white
collectionView?.register(shootDayCell.self, forCellWithReuseIdentifier: cellId)
}

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

return cell
}

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: CGFloat(view.frame.width), height: <#T##CGFloat#>)
}
}

class shootDayCell: UICollectionViewCell {

override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews(){
backgroundColor = UIColor.red
}
}

提前致谢!

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