I have never used UICollectionViewController
s, and I thought that they were somewhat similar to UITableViewController
s, but to my surprise, I cannot add UI elements to my custom UICollectionViewCell
s in the same way as I do with custom UITableViewCell
s.
In fact, I can add labels, buttons, etc. in the Interface Builder, but when I run the application, the cells appear empty.
I have registered the cell class during the viewDidLoad
method by calling (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier
, and I've checked that I am returning a valid instance of UICollectionViewCell
in the (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
method.
What am I doing wrong?
When would you choose to use a collection view rather than a table view? Suggested approach: Collection views are there to display grids, but also handle entirely custom layouts, whereas table views are simple linear lists with headers and footers.
Select the Main storyboard from the file browser. Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices.
A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually. Collection views are a collaboration between many different objects, including: Cells. A cell provides the visual representation for each piece of your content.
My full explanation is here.
Create a custom class for the UICollectionViewCell:
import UIKit
class MyCollectionViewCell: UICollectionViewCell {
// have outlets for any views in your storyboard cell
@IBOutlet weak var myLabel: UILabel!
}
In the storyboard make the cell use this class
And set the Identifier for the cell
Don't use the registerClass...forCellWithReuseIdentifier
in viewDidLoad
. But you will refer to it in collectionView...cellForItemAtIndexPath
:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! MyCollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.myLabel.text = self.items[indexPath.item]
cell.backgroundColor = UIColor.yellowColor()
return cell
}
Hook up the outlets from the Views in your storyboard cell to the MyCollectionViewCell
class.
@Suragch is spot on. I had this problem too: my UICollectionViewCell prototype's contents did not show in the cell when it got drawn.
The critical line is: "Don't use the registerClass...forCellWithReuseIdentifier in viewDidLoad". Really, don't. Not just "there is no need to", but "if you do register, it will stop your cell prototype from loading properly".
This only applies to Storyboard-based code, not .xib-based.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With