UICollectionViewController Error in Swift 3.0: must be initialized with a non-nil layout parameter

后端 未结 5 910
清酒与你
清酒与你 2021-01-14 08:03

I\'m new to iOS development. I\'ve been learning Swift, and today, I tried using UICollectionViewController.

My code is as follows:

 class ViewContro         


        
5条回答
  •  佛祖请我去吃肉
    2021-01-14 08:25

    You can set your code like this:

    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...
    
    }
    

提交回复
热议问题