简体   繁体   中英

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

I am trying to create a simple collectionView for the firs time following this tutorial

I have followed the steps so far and also made sure I didnt miss anything. The problem is that I am getting this error:

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

I have had a look at different posts with the same problem but I none of the answers seem to be fixing me code. The tutorial doesn't specify an initial layout size for the collectionView so I assumed that Xcode just made it the bounds of the view automatically so it didn't have a non-nil layout?

class shootDaysCollectionView: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    private let cellId = "cellId"

    override func viewDidLoad() {

    }

    // VIEW WILL APPEAR
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.navigationItem.title = "SHOOT DAYS"

        collectionView?.backgroundColor = UIColor.white
        collectionView?.register(shootDayCell.self, forCellWithReuseIdentifier: cellId) 
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! shootDayCell

        return cell
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: CGFloat(view.frame.width), height: <#T##CGFloat#>)
    }
}

class shootDayCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupViews(){
        backgroundColor = UIColor.red
    }
}

Thank you so much in advance!

Instead of instantiating the vc with

shootDaysCollectionView()

use

shootDaysCollectionView(collectionViewLayout: UICollectionViewFlowLayout())

if you want to configure

let lay = UICollectionViewFlowLayout()

lay...

shootDaysCollectionView(collectionViewLayout:lay)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM