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?
UICollectionViewCelland open the Attributes inspector. Make sure that you've enteredVacationCellin the field Identifier. - dfribVacationCell. Thanks for the reminder though, it is an easy one to forget. - Snooperlock