0

I'm trying to build a collection view in a standard view controller via storyboarding. I'm not using a collection view controller because I want several collection views on one screen. The view controller has the right class (statisticsViewController) and the Collection View is connected to the code (as outlet, dataSource and delegate)

My class code is following:

import UIKit

class statisticsViewController :
UICollectionViewController,
UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionViewActive: UICollectionView!
    private let reuseIdentifier = "cellIdentifier"

    var items: [String] = ["Bli", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb"]

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return items.count
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        println("ich war hier")
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count;
    }


    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
        cell.backgroundColor = UIColor.blackColor()

        return cell
    }
}

When I try to run the code I get following error message:

2014-12-18 16:31:47.533 project[2008:92664] *** Assertion failure in -[project.statisticsViewController loadView], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UICollectionViewController.m:166
2014-12-18 16:31:47.536 project[2008:92664] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "UIr-oH-mfu-view-fG7-sl-ulu" nib but didn't get a UICollectionView.'
*** First throw call stack:

....

When I set a break point I see that viewDidLoad isn't even called.

Does someone has an idea? Thank you for any help :)

LilK3ks
  • 205
  • 2
  • 3
  • 14
  • You said you didn't want to use a UICollectionViewController, so why did you make statisticsViewController a subclass of UICollectionViewController? – rdelmar Dec 18 '14 at 15:48
  • I did not want to use the UICollectionViewController you cen drag in in the storyboard. – LilK3ks Dec 18 '14 at 18:04
  • But you made statisticsViewController a subclass of UICollectionViewController, so you ARE using one. You should make statisticsViewController a subclass of UIViewController, and drag a collection view into its view (or several of them if that's what you want). – rdelmar Dec 18 '14 at 18:05
  • Oh uhhhmmm yaaa, now it works :D Its too late :D Thank you a lot rdelmar! – LilK3ks Dec 18 '14 at 18:24

1 Answers1

0

Just to make this answer answered :D

import UIKit

class statisticsViewController : UIViewController, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var collectionViewActive: UICollectionView!
private let reuseIdentifier = "cellIdentifier"

var items: [String] = ["Bli", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb", "Bla", "Blubb"]

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return items.count
}

override func viewDidLoad() {
    super.viewDidLoad()
    println("ich war hier")
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count;
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
    cell.backgroundColor = UIColor.blackColor()

    return cell
}

}

Now it works!

LilK3ks
  • 205
  • 2
  • 3
  • 14