1

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!

spoax
  • 271
  • 7
  • 20
  • Where is your collection view initialization code? – Rakesha Shastri Sep 02 '18 at 11:21
  • you are beginner, the first thing you do is to create the collection view in storyboard, when you get the enough understanding of the collection view then create the collection view in programmatically – Salman500 Sep 02 '18 at 11:26
  • check this link it might help, https://stackoverflow.com/questions/24288927/uicollectionview-must-be-initialized-with-a-non-nil-layout-parameter?rq=1 – Salman500 Sep 02 '18 at 11:26

1 Answers1

8

Instead of instantiating the vc with

shootDaysCollectionView()

use

shootDaysCollectionView(collectionViewLayout: UICollectionViewFlowLayout())

if you want to configure

let lay = UICollectionViewFlowLayout()

lay...

shootDaysCollectionView(collectionViewLayout:lay)
Sh_Khan
  • 86,695
  • 6
  • 38
  • 57
  • 1
    Thank you! Worked perfectly. I have missed out `UICollectionViewFlowLayout` when instantiating the vc. – spoax Sep 02 '18 at 11:37