1

I am using two UICollectionView in same controller but when I am initialising second UICollectionView it is Giving exception like

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

This is my code:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (collectionView==collView) {
    return arrDatabase.count;
}
else
    return arrSearchResult.count;
}    

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:     
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier=@"cell";

UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

if (collectionView==collView) {
    objprop=[arrDatabase objectAtIndex:indexPath.row];
    UIImageView *imgView=(UIImageView*)[cell viewWithTag:100];
    //imgRecipe.image=[UIImage imageNamed:[ objectAtIndex:indexPath.row]];
    imgView.image=[UIImage imageNamed:@"Hamburger.jpg"];
    UILabel *lblName=(UILabel*)[cell viewWithTag:101];
    lblName.text=[NSString stringWithFormat:@"%@",objprop.categoryName];
    //cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
}

if (collectionView==collView1) {
    NSString *str=@"setIdentifier";
    UIImageView *imgView2;
    UILabel *lblCat;
    UICollectionViewCell *cell = [collectionView
                                  dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    if (cell==nil) {
        cell
        =[[UICollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, 97, 101)];
        imgView2=[[UIImageView alloc]initWithFrame:CGRectMake(2, 13, 92, 76)];

        lblCat=[[UILabel alloc]initWithFrame:CGRectMake(3, 94, 92, 13)];
    }

    imgView2.image=[UIImage imageNamed:@"hamburger.jpg"];
    [cell.contentView addSubview:imgView2];
    lblCat.backgroundColor=[UIColor clearColor];
    lblCat.text=[NSString stringWithFormat:@"%@",[arrSearchResult[indexPath.row] objectAtIndex:0]];
    [cell.contentView addSubview:lblCat];

}

return cell;

}
Andriy
  • 2,767
  • 2
  • 21
  • 29
sanjeet
  • 1,539
  • 15
  • 37

2 Answers2

1

Please register the UICollectionViewCell class in viewDidLoad like:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
Andriy
  • 2,767
  • 2
  • 21
  • 29
Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
  • i am using two uicollectionview in same view controller .so i will have register two times for diffrent uicollectionview. – sanjeet Feb 15 '14 at 10:17
0

set your cell identifier as in code

enter image description here

codercat
  • 22,873
  • 9
  • 61
  • 85
  • reason: 'the collection view's data source did not return a valid cell from -collectionView:cellForItemAtIndexPath: for index path ,i got this error @iDev can you explain more – sanjeet Feb 15 '14 at 08:45
  • http://stackoverflow.com/questions/12657421/uicollectionview-cellforitematindexpath-not-registering-cell – codercat Feb 15 '14 at 08:54