【发布时间】:2018-09-02 11:19:17
【问题描述】:
我正在尝试在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
}
}
非常感谢您!
【问题讨论】:
-
你的集合视图初始化代码在哪里?
-
你是初学者,你要做的第一件事就是在storyboard中创建collection view,当你对collection view有了足够的了解后,再以编程方式创建collection view
-
检查此链接可能会有所帮助,stackoverflow.com/questions/24288927/…
标签: ios swift xcode uicollectionview