Gligor
Gligor

Reputation: 2940

UICollectionView with UIView as delegate and datasource

I must be a complete noob, but I can't figure this out and need some help.

I have a UIView with a UICollectionView in it. When I add the collection view, I don't get a UICollectionViewCell in my nib for some reason I only get this when I add the collection view in a UIViewController. Part of my question is - is it possible to have a UICollectionViewCell in my nib file here?

I assumed the answer was no so I tried the following:

I created a separate nib file and class for my custom UICollectionViewCell and registered the custom cell to my collection view like this:

[self.itemsCollection registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"ItemCell"];

then in my cellForItemAtIndexPath method, I do this:

CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ItemCell" forIndexPath:indexPath];
cell.itemImage.image = [UIImage imageNamed:@"Some Image.png"];
cell.itemLabel.text = @"Some Title";

however, when my cell gets created the itemImage and the itemLabel are both nil.

Any ideas would be appreciated!

Thanks.

Upvotes: 2

Views: 1349

Answers (1)

Rob
Rob

Reputation: 437592

If you've defined your IBOutlet references in the NIB, then you have to register the NIB, not the class. So, replace:

[self.itemsCollection registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"ItemCell"];

with

[self.itemsCollection registerNib:[UINib nibWithNibName:@"YourNibName" bundle:nil] forCellWithReuseIdentifier:@"ItemCell"];

Your NIB can specify the base class of CustomCollectionViewCell.

Upvotes: 1

Related Questions