Reputation: 71
I'm trying to setup an UICollectionView
. At first I was using the basic UICollectionViewCell
, it was working well, the cells were being displayed.
Then I created my own collectionViewCell (subclassing UICollectionViewCell
), to be able to display an image in the cell.
I linked the custom cell in the Storyboard with an identifier and I also specified the right class for it.
However, I keep having this error, and I don't understand why:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MediaCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I managed to take care of the bug by adding this line in the viewDidLoad
but in the tutorials I was following the guys were not using it:
[self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"MediaCell"];
The problem is that if I add it, the collectionView remains black and no data is displayed.
Upvotes: 2
Views: 4768
Reputation: 71
It was actually a problem related with the TabBarController. I wasn't displaying the right class inside it that's why the collectionView was always empty. The code I posted here was working. Thank you everyone for your help.
Upvotes: 1
Reputation: 2187
You're right in using the line
[self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"MediaCell"];
as this helps with cell reuse.
The problem could be your cellForRowAtIndexPath
method, or your subclass, or the way you set up your delegates. Can you post the code?
Your delegate method should look something like this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MediaCollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MediaCell" forIndexPath:indexPath];
// set cell.image
return cell;
}
You should set up your delegate in your viewDidLoad method with...
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
EDIT: Can you post the code for your MediaCollectionViewCell implementation? I did something similar, and mine looks like this.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
[self.contentView addSubview:self.imageView];
}
return self;
}
Upvotes: 2
Reputation: 5824
I had this exact issue yesterday (as the documentation says you MUST register the class), but the documentation is inaccurate in that you should not register if a prototype cell exists in your storyboard and you give it an identifier.
If you create a prototype of the UICollectionViewCell
in your storyboard and assign your custom cell class, it is not necessary (and is detrimental if you do) register the custom UICollectionViewCell
. This is the reason the resulting UICollectionView
is black.
Based on the error message, the prototype cell in the storyboard does not have its identifier set correctly (either not set at all or is not MediaCell
)
Upvotes: 6