Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS UICollectionView Register nib for UICollectionViewCell not working

here's my code :

- (void)viewDidLoad {
    [super viewDidLoad];
   ...
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    [layout setItemSize:CGSizeMake(160, 160)];
    [layout setMinimumInteritemSpacing:5.f];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

    _photoPicker = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10 , self.view.frame.size.width - 20, 160 ) collectionViewLayout:layout];
    [_photoPicker registerNib:[UINib nibWithNibName:@"photoUploadCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@" photoUploadCell"];

    //[_photoPicker registerClass:[photoUploadCollectionViewCell class] forCellWithReuseIdentifier:@"photoUploadCell"];
    _photoPicker.backgroundColor = gray;
    _photoPicker.delegate = self;
    _photoPicker.dataSource = self;
    [self.scrollView addSubview:_photoPicker];

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

}

This code crash at photoUploadCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath]; and I get this error :

2015-05-26 10:45:29.363 Free Sale[4505:55482] * Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /SourceCache/UIKit_Sim/UIKit-3347.44/UICollectionView.m:3454 2015-05-26 10:45:29.374 Free Sale[4505:55482] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier photoCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' *** First throw call stack:

Any one know this wired issue ?

like image 236
Chlebta Avatar asked May 26 '15 09:05

Chlebta


3 Answers

photoCell is not registered.. is this the actual code you have? because you have a white space forCellWithReuseIdentifier: remove the white space..

Happy coding, Cheers!

like image 188
0yeoj Avatar answered Nov 17 '22 09:11

0yeoj


Notice the code :

[_photoPicker registerNib:[UINib nibWithNibName:@"photoUploadCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@" photoUploadCell"];

Remove the white space from @" photoUploadCell"

Hope this will fix your problem

like image 42
Ajith Kumar Avatar answered Nov 17 '22 10:11

Ajith Kumar


You registered it with a different namephotoUploadCell, than the one you use later on photoCell.

like image 1
pteofil Avatar answered Nov 17 '22 08:11

pteofil