2
votes

I have a very simple collection view, but somehow it keeps crashing. here is the error message:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Reuse - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

and my codes:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseCellIdentifier = @"Reuse";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseCellIdentifier forIndexPath:indexPath];

    if (!cell) {
        cell = [[UICollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, 106, 95)];
    }

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 45, 45)];

    imageView.image = [UIImage imageNamed:@"image_name"];
    [cell addSubview:imageView];

    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 106, 15)];
    name.text = @"user";

    return cell;
}

Help!!

3
Give cell identifier as "Reuse" inside Collection Reusable View section. - cjd

3 Answers

2
votes

seems like you didn't declare your cell reuse identifier on your storyboard, do these steps:

- select the cell on your storyboard

enter image description here

- Enter the reuse identifier into the "Collection Reusable view identifier", in your case enter "Reuse" there

enter image description here

Hope that helps..

2
votes

use this and if not set delegate. please your delegates.

Prior to calling the dequeueReusableCellWithReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerNib:forCellWithReuseIdentifier: method to tell the collection view how to create a new cell of the given type. If a cell of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a new cell object automatically.

  [self.yourcollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

enter image description here

apple doc

0
votes

check out this link.. http://www.appcoda.com/ios-programming-uicollectionview-tutorial/. Give the Identifier(Reuse) name and static NSString *reuseCellIdentifier = @"Reuse"(this name); should be same ..