What does it mean UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:?

11,473

Solution 1

You need to register your cell using one of the relevant registration methods provided in the UICollectionView Class Reference

- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;

The relevant one of these only needs to be called once per instance of a UICollectionView.

Solution 2

In case anyone hits this, another possibility is if you forget to return cell; at the end of the collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath method.

Solution 3

Without storyboard you need to register your cell inside viewDidLoad :

// Register Nib
[self.collectionView registerNib:[UINib nibWithNibName:CollectionViewCell_XIB bundle:MAIN_BUNDLE] forCellWithReuseIdentifier:CollectionViewCell_ID];

CollectionViewCell_XIB is the name of your cell xib

CollectionViewCell_ID is the identifier of your cell xib

Share:
11,473

Related videos on Youtube

user4951
Author by

user4951

Updated on June 17, 2022

Comments

  • user4951
    user4951 about 2 years

    Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]

    I got this error since IOS 7. It works fine in IOS 6.

    I look around the web and I found this:

    http://forums.xamarin.com/discussion/8233/ios-7-crash-in-collectionview

    However, the solution doesn't make sense

    I figured it out. I had incorrectly used CollectionView.RegisterClassForCell. Apparently I was supposed to use CollectionView.RegisterNibForCell when setting up the viewcontroller. That fixed the problem. iOS 6 must have been more forgiving.

    Could any of you give me a hint on this bug

    Symptomps:

    1. Crash
    2. Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]
    3. Works in IOS6 and not IOS 7

    The code I am suspicious with is this:

    - (CGSize)collectionView:(UICollectionView *)collectionView
                      layout:(UICollectionViewLayout*)collectionViewLayout
      sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        CGSize sz = [BGCollectionViewCellImage defaultSize];
        return sz;
    }
    

    But it seems to be too ordinary.

    sz is simply CGSize of 100*120

    Another is this:

    - (BGCollectionViewCellImage *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        BGCollectionViewCellImage * cell = [[BGCollectionViewCellImage alloc]init];
        Image * img= self.arImages[indexPath.row];
        [cell setImg:img andIndex:indexPath.row+1];
    
        return cell;
    }
    

    Maybe I should use dequeue something like in UITableViewCell

    I got another hint. If I tried to dequeue some cell, I got this:

    2013-10-14 21:18:34.346 domainname[24667:a0b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier BGCollectionViewCellImage - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' * First throw call stack: (

    Looks like I got to register something first.

  • Harrison Xi
    Harrison Xi over 8 years
    Looks like that UICollectionView must create cell via reuse identifier.
  • Liron
    Liron over 7 years
    @iNasir glad this helped.