user3309150
user3309150

Reputation: 25

error with UICollectionView

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!!

Upvotes: 2

Views: 558

Answers (3)

codercat
codercat

Reputation: 23301

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

Upvotes: 2

Xu Yin
Xu Yin

Reputation: 3940

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..

Upvotes: 2

gajchtr
gajchtr

Reputation: 48

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 ..

Upvotes: 0

Related Questions