Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing UICollectionViewCell in storyboard

I have never used UICollectionViewControllers, and I thought that they were somewhat similar to UITableViewControllers, but to my surprise, I cannot add UI elements to my custom UICollectionViewCells in the same way as I do with custom UITableViewCells.

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 UICollectionViewCellin the (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath method.

What am I doing wrong?

like image 534
neutrino Avatar asked Jan 14 '15 15:01

neutrino


People also ask

What is the difference between Tableview and CollectionView?

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.

How do I create a custom CollectionView in Swift?

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.

What is a collection view?

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.


2 Answers

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

enter image description here

And set the Identifier for the cell

enter image description here

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.

like image 118
Suragch Avatar answered Oct 31 '22 01:10

Suragch


@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.

like image 43
Ben Avatar answered Oct 31 '22 02:10

Ben