本文介绍了UICollectionViewController Swift 3.0中的错误:必须使用非零布局参数进行初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to iOS development.I've been learning Swift, and today, I tried using 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
    }


}

然而,当我尝试运行它时,我得到了以下错误:

However, when I try to run it, I get the following error:

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

我已查阅以前的帖子讨论此问题,但我不能找到任何修复我的代码的东西。

I have looked up previous posts discussing this issue, but I couldn't find anything that fixed my code.

我该如何解决这个问题?

How should I fix this?

推荐答案

谢谢!我收录了你的建议。下面是有效的代码:

Thank you! I incorporated your suggestions. Below is the code that works:

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

Again, thank you guys very much. Have an awesome day :D

这篇关于UICollectionViewController Swift 3.0中的错误:必须使用非零布局参数进行初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:57