Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView: Unrecognized selector sent to instance

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;
 }
like image 708
user3249524 Avatar asked Dec 15 '22 01:12

user3249524


2 Answers

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.

like image 140
Aaron Brager Avatar answered Dec 17 '22 13:12

Aaron Brager


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
like image 41
Clay Bridges Avatar answered Dec 17 '22 14:12

Clay Bridges