【问题标题】:UICollectionViewController Error in Swift 3.0: must be initialized with a non-nil layout parameterSwift 3.0 中的 UICollectionViewController 错误:必须使用非零布局参数进行初始化
【发布时间】:2016-11-04 07:17:40
【问题描述】:

我是 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'

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

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

【问题讨论】:

  • 请粘贴堆栈跟踪

标签: ios swift uicollectionview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let cellSize = CGSize(width: 90, height: 120)
        //
        return CGSizeMake(cellSize)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    
        return UIEdgeInsetsMake(top: 20, left: 10, bottom: 10, right: 10)
    }
    

    【讨论】:

      【解决方案3】:

      这应该可行:

      lazy var collectionView: UICollectionView = {
           let layout = UICollectionViewFlowLayout()
           let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
           return collectionView
      }()
      

      【讨论】:

        【解决方案4】:

        只需像下面这样更改顺序:

        let layout = 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)
        self.view.addSubview(colView)
        colView.delegate   = self
        colView.dataSource = self
        colView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        colView.backgroundColor = UIColor.white
        

        【讨论】:

          【解决方案5】:

          你可以这样设置你的代码:

          class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
          
              let cellId = "Cell"
          
              lazy var collectionView: UICollectionView = {
                  let layout = UICollectionViewFlowLayout()
                  // ... layout parameters
                  let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
                  cv.backgroundColor = .white
                  cv.delegate = self
                  cv.dataSource = self
                  return cv
              }()
          
              override func viewDidLoad() {
                  super.viewDidLoad()
          
                  view.addSubview(collectionView)
                  // ... add your auto layout constraints to the collection view
                  collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
              }
          
              // Delegate and Data Source Methods...
          
          }
          

          【讨论】:

            猜你喜欢
            • 2018-05-09
            • 2019-09-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-08-08
            • 1970-01-01
            • 1970-01-01
            • 2019-01-19
            相关资源
            最近更新 更多