I am getting this error. *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell label]: unrecognized selector sent to instance 0x1eead660'
I am using a nib file as my cell and trying to displays the cells correctly. I am guessing that I am not returning cells correctly, but I am not too sure. Any help will be appreciated.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
NSMutableArray *data = [sections objectAtIndex:indexPath.section];
cell.label.text = [data objectAtIndex:indexPath.item];
return cell;
}
UICollectionViewCell doesn't have a property called label
.
Perhaps you meant:
[self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
Assuming you subclassed UICollectionViewCell
, added label
, and called your subclass Cell
.
Your nib file probably needs to be connected to your custom class ('Cell'?) somehow. That done, you'd call:
[self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
As it stands right now, you are getting a vanilla UICollectionViewCell
object back from dequeue...
, and when you try to use it as a Cell
, you get problems.
BTW, generally, your registerClass
code should not go in cellForItemAtIndexPath
. It only needs to be called once per view, e.g.
static NSString *cellIdentifier = @"Cell";
@implementation YourCollectionViewController
// ...
- (void)viewDidLoad
{
[self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];
}
// ...
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With