user3689341 user3689341 - 7 months ago 22
Swift Question

UICollectionViewController shows up blank/black

I am trying to go through some collection view Swift tutorials, but they always show up as blank/black pages as if I have not set an initial view controller. However I have, I only drug out 1

UICollectionViewController
and set the custom class to my
CollectionViewController
. In this class I implemented the
numberOfSectionsInCollectionView:
method and just to make sure it is working I just returned 3. However when I build and run the project all that should up is a blank/black screen...

Can someone please help? I have been searching through info with no luck...

Answer

You have to implement at least 3 of the UICollectionViewDataSource methods to get anything to display.

It sounds like you are only telling it how many sections you want, but you have not told it how many views in each section or provided views for it to display.

Try this:

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 2
    }

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = UICollectionViewCell()
        cell.backgroundColor = UIColor.redColor()
        return cell
    }