0
votes

I tried to create a dummy photo app with UICollectionView. My Xcode version is 7.2 and Swift version 2.1.1. I use Storyboard to build the UI part by following the tutorial from http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1

In Swift 2.0, the UICollectionViewDataSource is inherited from UICollectionViewController, we don't need to explicitly declare those protocols. I implemented the required override methods for DataSource, and also register the customized cell in viewDidLoad() in the UICollectionViewController. I put a test Label in my cell to check whether it works or not. Unfortunately, the label never appear after I launch the app. I attach some of my code below as reference:

UICollectionViewController

class VacationsCollectionViewController: UICollectionViewController {

    private let reuseIdentifier = "VacationCell"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(VacationsCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    // MARK: UICollectionViewDataSource

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

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

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

        // Configure the cell
        cell.backgroundColor = UIColor.blueColor()
        cell.CellText.text = "Show me the money"

        return cell
    }
}

UICollectionViewCell

class VacationsCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var CellText: UILabel!
}

Any idea why the label never show up in my dummy app?

1
In your storyboard, select your UICollectionViewCell and open the Attributes inspector. Make sure that you've entered VacationCell in the field Identifier. - dfrib
@dfri Yes, the identifier is there with value VacationCell. Thanks for the reminder though, it is an easy one to forget. - Snooperlock

1 Answers

0
votes

It looks like you have everything you need. The issue is probably with your storyboard. Just to be safe, add constraints to the label so it's definitely going to be displayed properly. Then, add an extension to your collection view controller and be sure the size allows the label to be displayed as well. This, at least, worked for me and I was in your exact situation.

extension OfficesCollectionViewController: UICollectionViewDelegateFlowLayout{
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 200, height: 200)
    }
}