Saman
Saman

Reputation: 523

Error : "could not dequeue a view of kind" in Objective-C

I Created a UICollectionViewController in StoryBoard and set it's class to CollectionViewController1 and i have a ReuseIdentifier.

Here is my error :

Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:
forIndexPath:viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit
/UIKit-3600.8.1/UICollectionView.m:5115

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

And it's my code :

#import "CollectionViewController1.h"

@interface CollectionViewController1 ()
{
    UICollectionView *collectionview;
}
@end

@implementation CollectionViewController1

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = self.view.frame.size;
    collectionview = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionview.pagingEnabled = TRUE;
    collectionview.translatesAutoresizingMaskIntoConstraints = NO;
    collectionview.delegate = self;
    collectionview.dataSource = self;
    collectionview.bounces = false;
    [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.view addSubview:collectionview];
}
#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    // Configure the cell

    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                        layout:(UICollectionViewLayout *)collectionViewLayout
        insetForSectionAtIndex:(NSInteger)section;
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size;
}

Upvotes: 1

Views: 534

Answers (3)

user8263284
user8263284

Reputation:

Your crash gives a different class to the one you register:

UICollectionElementKindCell

vs

UICollectionViewCell

Change one of these to the class you want - I'm guessing the latter.

Upvotes: 0

tailor
tailor

Reputation: 665

I think you have another UICollectionView in storyboard with empty cell identifier

Upvotes: 1

Saman
Saman

Reputation: 523

Add self.collectionView = collectionview; after create collection view.

Upvotes: 0

Related Questions