Fudgey
Fudgey

Reputation: 3833

Using UICollectionViewController within Container - Swift

Im trying to use a UICollectionViewController embedded within a container on my MainViewController

Shown below:

enter image description here

Here is my CollectionViewController:

class ExistingProjectsCollectionViewController: UICollectionViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 1
    }

    override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return 1000
    }

    override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell

        // Configure the cell

        return cell
    }

However, for some reason when i run the application, the CollectionView scrolls, but contains no orange boxes.

Am i missing something obvious ?

Upvotes: 1

Views: 2816

Answers (1)

matt
matt

Reputation: 535159

The problem is probably this line:

self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

You do not need to, and should not, register any class merely in order to get cells out of the storyboard. The storyboard can specify a cell class if needed. Basically you are saying "do NOT get the cells from the storyboard - just use a plain vanilla UICollectionViewCell instead", which is the exact opposite of what you want to do.

I created a project set up as you describe (embed segue from inside my main view controller), copied and pasted your code into into, commented out that line, and ran it. The cells appeared just fine.

Upvotes: 1

Related Questions