Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a Collection View Cell from storyboard (instead of from nib with registerNib)

In Xcode 5.0.2 I'm trying to recreate Michael Lehman's "Collection View" example for his video tutorial Learning To Build Apps For iPhone And iPad (password protected on Safari; the chapter 5).

Michael loads a Collection View Cell from a nib file with the following code (here the full version of his ExploreUICollectionView/ViewController.m):

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;

    self.cellData = [[NSMutableArray alloc]init];

    for(int i=0; i < 100; i++)
    {
        [self.cellData addObject:[NSString stringWithFormat:@"#%d", i+1]];
    }

    UINib *cellNib = [UINib nibWithNibName:@"CVCell"
                                    bundle:nil];
    [self.myCollectionView registerNib:cellNib
            forCellWithReuseIdentifier:@"CVCell"];
}

I however would like not to create a separate nib file for the cell, but drag a View to the Main_iphone.stroryboard and Main_ipad.storyboard, give it a "Storyboard ID" if needed and then (somehow) use it in myCollectionView.

My question is which methods should I call then please?

I can not use a nibWithNibName and registerNib:forCellWithReuseIdentifier here.

UPDATE: rdelmar's advice has worked (thanks!):

enter image description here

like image 807
Alexander Farber Avatar asked Dec 11 '22 09:12

Alexander Farber


1 Answers

You can't drag a view into a storyboard, only view controllers. To do it in a storyboard, you add a collection view to a controller, or drag out a collection view controller. You will get a single cell in that collection view, to which you can add any subviews you want. Give the cell a reuse identifier, and don't register anything in your controller code. You can make a subclass of UICollectionViewCell if you want, and change the class of that cell to your subclass.

like image 160
rdelmar Avatar answered Dec 25 '22 22:12

rdelmar