I\'m trying to add a UICollectionView to a .nib file in IB. I\'ve worked through a few tutorials and had no problems.
In my existing app, when I drag a collection vi
It's been a while since the question has been asked but I faced the same issue lately.
Eventually, I found the answer here.
In short:
You cannot drag and drop a UICollectionViewCell into a UICollectionView if you are in a .xib file. This is only possible in a storyboard.
The workaround is to create a .xib file for your custom cell and to register the cell manually using:
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
Define your cell view in a separate nib file. The view must be of type UICollectionViewCell
or a subclass.
Then, you must register the nib with your collection view:
- (void)viewDidLoad
{
UINib *cellNib = [UINib nibWithNibName:@"MyNib" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cell"];
}
I can't tell you why it does not work, but for me the subclass approach from this blog post solved the problem:
http://www.adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial.php
here is a short summary:
@interface MyViewCell : UICollectionViewCell
go back to your custom class and modify the inithWithFrame method to load and use your created view.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CVCell" owner:self options:nil];
if ([arrayOfViews count] < 1) { return nil; }
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { return nil; }
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
then you can register this class to be used by the collectionView with
[self.collectionView registerClass:[MyViewCell class] forCellWithReuseIdentifier:@"myCell"];
While these answers are correct, the underlying cause of the problem is quite subtle and Apple needs to update their error message in the log. When you see:
Unknown class MyViewCell in Interface Builder file.
What it actually means is that your source code is missing the @implementation declaration or that the .m file has not been added to the target. So check your code and make sure you have:
@implementation MyViewCell
@end
This is true for both UITableViewCell and UICollectionViewCell.
I discuss the problem further at https://stackoverflow.com/a/12755891/539149 for the general case.
The interesting thing is that if you are missing the @implementation and call:
[self.myCollectionView registerClass:[MyViewCell class] forCellWithReuseIdentifier:@"MyViewCell"];
You will see:
Undefined symbols for architecture armv7:
"_OBJC_CLASS_$_MyViewCell", referenced from:
objc-class-ref in MyViewController.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So Apple knows about the problem, they just aren't detecting it during compilation. I would advise against putting custom cells in separate nibs or calling registerClass: forCellWithReuseIdentifier:. It's much cleaner to store the custom cell inside of its container UICollectionView or UITableView in Interface Builder.