0

I have created collection view programmatically and I am adding .xib file in collection view cell like below

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const reuseIdentifier = @"CollectionCell";

    CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.itemImage.image = [UIImage imageNamed:images[indexPath.row]];
    cell.itemLabel.text = [NSString stringWithFormat:@"Item Index %ld",(long)indexPath.row];
    return cell;
}

But it's showing exception like

could not dequeue a view of kind: UICollectionElementKindCell with identifier CollectionCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

What did I do here wrong??? Help me please.

Bharat Nakum
  • 647
  • 6
  • 18

0 Answers0