代码之家  ›  专栏  ›  技术社区  ›  Anand Rockzz

即使在提供布局之后,UICollectionViewDelegateFlowLayout也出现错误

  •  0
  • Anand Rockzz  · 技术社区  · 5 年前

    'UICollectionView must be initialized with a non-nil layout parameter'
    

    使用此ViewController

    import UIKit
    
    class SelectDesignController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
        private let designCellId = "designCellId"
    
        override func viewDidLoad() {
            super.viewDidLoad()
            configureNavBar()
            configureUI()
            collectionView.backgroundColor = #colorLiteral(red: 0.8515156507, green: 0.9521961808, blue: 0.9503603578, alpha: 1)
            collectionView.register(DesignCategoryCell.self, forCellWithReuseIdentifier: designCellId)
        }
        @objc func handleDismiss() {
            dismiss(animated: true, completion: nil)
        }
    
        func configureNavBar() {
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage.fontAwesomeIcon(name: .chevronLeft, style: .solid, textColor: .white, size: CGSize(width: 40, height: 40)).withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
        }
        func configureUI (){
            view.backgroundColor = #colorLiteral(red: 0.8515156507, green: 0.9521961808, blue: 0.9503603578, alpha: 1)
        }
        override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: designCellId, for: indexPath) as! DesignCategoryCell
            return cell
        }
        override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 1
        }
        /** PROVIDING a LAYOUT HERE **/
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: view.frame.width, height: view.frame.height)
        }
    
    }
    

    我包括 SelectDesignController

    let controller = SelectDesignController()
    let nav = UINavigationController(rootViewController: controller)
    configureNav(nav: nav, title: "Designs")
    present(nav, animated: true, completion: nil)
    

    知道吗,我错过了什么?

    1 回复  |  直到 5 年前
        1
  •  0
  •   Mr.Javed Multani    5 年前

    你应该检查两件事:

    框架

    集合视图的边框矩形,以点为单位。框架的原点相对于要在其中添加它的超视图。此帧在初始化期间传递给超类。

    用于组织项的布局对象。集合视图存储对指定对象的强引用。不能为零。