ios - Swift 3.0 中的 UICollectionViewController 错误 : must be initialized with a non-nil layout parameter

标签 ios swift uicollectionview

我是 iOS 开发新手。 我一直在学习 Swift,今天,我尝试使用 UICollectionViewController。

我的代码如下:

 class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    var colView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 90, height: 120)

        colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        colView.dataSource = self
        colView.delegate = self
        colView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        colView.backgroundColor = UIColor.white
        self.view.addSubview(colView)
    }

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
        cell.backgroundColor = UIColor.orange
        return cell
    }


}

但是,当我尝试运行它时,出现以下错误:

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

我查阅了以前讨论此问题的帖子,但找不到任何可以修复我的代码的内容。

我应该如何解决这个问题?

最佳答案

谢谢!我采纳了你的建议。以下是有效的代码:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    let cellId = "CellId"
    var colView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 111, height: 111)
        
        colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        colView.delegate   = self
        colView.dataSource = self
        colView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        colView.backgroundColor = UIColor.white
        
        self.view.addSubview(colView)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 21
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
        cell.backgroundColor = UIColor.orange
        return cell
    }
}

再次感谢大家。祝你有美好的一天:D

关于ios - Swift 3.0 中的 UICollectionViewController 错误 : must be initialized with a non-nil layout parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40417252/

相关文章:

swift - 找不到 addQuadCurve 的文档(至 :control)

ios - 滚动时 UISwitch 在 Collection View 单元格中复制/奇怪的覆盖

ios - Collection View 中单元格的自定义填充顺序

ios - 如果 "active"列在 Swift 3/Xcode 8 中为 "no",如何隐藏单元格

ios - 仅为 ios swift 2.3 和 swift 3 中的特定 View Controller 更改方向

iphone - 检查服务器是否有匹配的 UDID 并使用匹配的用户名自动填充 UITextField

swift - 从带有 swift map 功能的选项开始

ios - UICollectionView 自定义布局问题与 layoutAttributesForElementsInRect :rect

ios - UICollectionView Cell Spacing 基于设备屏幕尺寸

javascript - 预期的不合格 ID cstdlib