Mrugesh Tank
Mrugesh Tank

Reputation: 3560

Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] in ios 7

I getting this error while using two UICollectionView. i search for this error and get solution that collection view should initialise with registerNib but in my coding i have already done this. my viewDidLoad looks like this

UINib *cellNib =[UINib nibWithNibName:@"myCell" bundle:nil];
[self.horizontalCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"myCell"];

UICollectionViewFlowLayout *horizontalFlowLayout=[[UICollectionViewFlowLayout alloc]init];
[horizontalFlowLayout setItemSize:CGSizeMake(300, 150)];
[horizontalFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.horizontalCollectionView setCollectionViewLayout:horizontalFlowLayout];

UINib *cellNib1 =[UINib nibWithNibName:@"myCell1" bundle:nil];
[self.horizontalCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"myCell"];
UICollectionViewFlowLayout *verticalFlowLayout=[[UICollectionViewFlowLayout alloc]init];
[verticalFlowLayout setItemSize:CGSizeMake(150, 150)];
[verticalFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
NSArray *a=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
[self.verticalCollectionView setCollectionViewLayout:verticalFlowLayout];

self.dataArray=[[NSArray alloc]initWithObjects:a, nil];

and cellForItemAtIndexPath method is looks like below.

if (collectionView==self.horizontalCollectionView)
{

NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];

NSString *cellData = [data objectAtIndex:indexPath.row];

static NSString *cellIdentifier = @"myCell";

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

UILabel *titleLabel = (UILabel *)[cell viewWithTag:1];
titleLabel.textColor=[UIColor whiteColor];
[titleLabel setText:cellData];

return cell;
}
else
{
    NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];

    NSString *cellData = [data objectAtIndex:indexPath.row];

    static NSString *cellIdentifier1 = @"myCell";

    UICollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier1 forIndexPath:indexPath];

    UILabel *titleLabel = (UILabel *)[cell1 viewWithTag:2];
    titleLabel.textColor=[UIColor whiteColor];
    [titleLabel setText:cellData];

    return cell1;
}

getting error while initialising cell1.

Full error message is as below. * Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /SourceCache/UIKit_Sim/UIKit-2935.137/UICollectionView.m:3241

Upvotes: 2

Views: 8395

Answers (4)

Hyde
Hyde

Reputation: 1

- Did you used Xib or Storyboard for cell?

  • I find if I Used Xib and in xib named it's Identifier "cell"

  • But I Used
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuseIdentifier"] in my code;

  • this Crashed

I change xib's Identifier as the same in my code, it run as well;

Upvotes: 0

Cubanishe
Cubanishe

Reputation: 15

You should add to viewDidLoad method the following lines:

  UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
  [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"CollCell"]; 

Where CollectionViewCell is the name of the custom CollectioViewCell nib file, and CollCell is the identifier you use to dequeue the Cell in (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath method

I hope this help you

Upvotes: -1

Jessicardo
Jessicardo

Reputation: 869

So I spent a total of 3 hours with this stupid issue. For me it was bad because other cells i did the exact same way worked except this one. This error is misleading. What it was for me was i had a GestureRecognizer in the xib... YUP it doesn't even have to be linked. Once i took that out it worked. I ended up just adding the gesture recognizer via code. Odd because it works fine for custom UITableViewCell but not custom UICollectionViewCell.

Hope this helps anyone!

Upvotes: 2

Steve Waddicor
Steve Waddicor

Reputation: 2217

I've just had this. For me the issue was:

Collection Reusable View Identifier in the nib was set to something different than the identifier I had registered in code with [self.collectionView registerNib:nib forCellWithReuseIdentifier:aNib];

Upvotes: 4

Related Questions