Reputation: 1510
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;
}
Upvotes: 1
Views: 2006
Reputation: 2077
Please register the UICollectionViewCell
class in viewDidLoad
like:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
Upvotes: 1