在此tutorial
之后,我正在尝试创建一个简单的collectionView到目前为止,我已经执行了所有步骤,并确保我没有错过任何内容。问题是我收到此错误:
由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ UICollectionView必须使用非空布局参数初始化”
我看过同样问题的不同文章,但我的答案似乎都没有解决我的问题。本教程没有为collectionView指定初始布局大小,因此我认为Xcode只是自动将其作为视图的边界,因此它没有非nil布局?
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
}
}
非常感谢您!
答案 0 :(得分:5)
不是用
实例化vc。shootDaysCollectionView()
使用
shootDaysCollectionView(collectionViewLayout: UICollectionViewFlowLayout())
如果要配置
let lay = UICollectionViewFlowLayout()
lay...
shootDaysCollectionView(collectionViewLayout:lay)