-2

I tried to do what all the other questions seem to do, but I still get the error. Maybe I'm missing something obvious but I looked over the documentation and all it seems is you need a layout to initialize.

let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
collectionView = UICollectionView(frame: view.frame, collectionViewLayout: flowLayout)
collectionView?.delegate = self
collectionView?.dataSource = self
collectionView?.register(InterestCollectionViewCell.self, forCellWithReuseIdentifier: interestReuseIdentifier)
view.addSubview(collectionView!)

Collection View Delegate Method

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

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let vc = collectionView.dequeueReusableCell(withReuseIdentifier: interestReuseIdentifier, for: indexPath) as! InterestCollectionViewCell
    vc.interestLabel.text = interests[indexPath.row].rawValue as String
    return vc
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.size.width, height: 45)
}
TrickSpades
  • 62
  • 1
  • 10
  • 2
    show your collectionview Delegates method – Himanshu Moradiya Oct 20 '16 at 04:13
  • It seems like u have delegate issue. Update your delegate with this UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout – Joe Oct 20 '16 at 04:17
  • You missing layout item size from your code....layout.itemSize = CGSize(width: 100, height: 100) – Joe Oct 20 '16 at 04:19
  • neither worked. – TrickSpades Oct 20 '16 at 04:24
  • 1
    Did you set those delegate in your class than this should work..i wondering can you explain what is this means width: view.frame.size.width. You should you tableView if you want to show one item/row.... – Joe Oct 20 '16 at 04:39
  • I want small little boxes, but I added the sizeForItemAt to see if that would fix it, which it did not. – TrickSpades Oct 20 '16 at 04:40
  • What kind of layout r u trying.can you show us a screenshot of your problem... – Joe Oct 20 '16 at 04:55
  • I'm trying to do something along the lines of this. https://i.gyazo.com/2bd89777e9b59cb6c21496798a80d058.png – TrickSpades Oct 20 '16 at 05:41
  • It's not collectionView. it's just a bunch of custom uilabel. You have to try something else... – Joe Oct 20 '16 at 06:40
  • That could totally be made in a Collection View and still doesn't solve my problem of the fact that I'm getting a nil collection view. – TrickSpades Oct 20 '16 at 18:19

1 Answers1

0

The solution was that I had to initialize the collectionView before presenting the UICollectionViewController. And I had to register my cell class in viewWillAppear as opposed to viewDidLoad.

TrickSpades
  • 62
  • 1
  • 10
  • 4
    It's great that you answered your own question. But please edit to show the code that has changed. – MwcsMac Jan 19 '17 at 21:15