8
votes

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 ?

4
Check the reuse-identifier and nib name is spelt correctly?Robert J. Clegg
I've checked that many times.Chlebta
And _photoPicker is definitely not nil, right?Robert J. Clegg
photoCell is not registered.. is this the actual code you have? because you have a white space forCellWithReuseIdentifier:..0yeoj
@0yeoj You just saved my day I was stucked here for about 2 hours thank's a lot Post your comment in answerChlebta

4 Answers

10
votes

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!

7
votes

Notice the code :

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

Remove the white space from @" photoUploadCell"

Hope this will fix your problem

1
votes

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

0
votes

1 : First modified your code with this

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

    }

2 : in this code

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

in @" photoUploadCell" there is space with Identifier make sure space are remove and match with nib and cell and register with same Identifier. cheers