4

I have UICollectionView, but it seems that I set up everything right. But I get the error:

'could not dequeue a view of kind: UICollectionElementKindCell with identifier PeopleCellForList  - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

My storybord:

enter image description here

enter image description here

enter image description here

My code is:

@interface PeopleCellForList : UICollectionViewCell

@end

@implementation PeopleCellForList

@end


#pragma mark - UICollectionView Datasource

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    return self.arrayPeople.count;
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"PeopleCellForList " forIndexPath:indexPath];
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {

}

#pragma mark – UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat width = 106;
    CGFloat height = width;
    if (indexPath.row % 3 == 2) {
        width = 108;
    }
    return CGSizeMake(width, height);
}

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

I tried [self.collectionViewMain registerClass:[PeopleCellForList class] forCellWithReuseIdentifier:@"PeopleCellForList"] in viewDidLoad: (during that I tried to remove and not remove cell from storyboard), but that didn't help.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Paul T.
  • 4,938
  • 7
  • 45
  • 93
  • You have to call `registerClass:...` method on collection view. Check if your collection is not `nil` when you're invoking the method. – Maciej Oczko Apr 18 '14 at 06:52
  • I checked it's not nil, and I tried to call registerClass, pls see my bottom update – Paul T. Apr 18 '14 at 06:53

1 Answers1

18

You have an extra space in @"PeopleCellForList "

Matías R
  • 2,195
  • 1
  • 14
  • 12
  • I put mine in quotation marks in the class field. .. doh! (Dear apple, please can you include field validation in xcode?- thx. ) – UKDataGeek Aug 23 '15 at 23:06
  • Fun fact: I had the same problem too, and I was following the tutorial at https://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12. And it too have a space in the cell identifier... :-) – Kalle Mar 17 '16 at 08:00