iOS Assertion Failure in UICollectionView

后端 未结 7 1360
抹茶落季
抹茶落季 2020-12-17 08:07

I\'m getting the error ...

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-237         


        
相关标签:
7条回答
  • 2020-12-17 08:45

    Been reading the docs (should possibly have done this first :) )

    Anyway, the collectionView I am using is within a separate xib file (not a storyboard) and from the docs...

    Important: You must register a class or nib file using the
    registerClass:forCellWithReuseIdentifier: or
    registerNib:forCellWithReuseIdentifier: method before calling this method.
    

    Thanks

    0 讨论(0)
  • 2020-12-17 08:57

    Make sure that if you use the registerNib: method:

    UINib *nibH = [UINib nibWithNibName:HEADER_ID bundle:nil];
    [collectionView registerNib:nibH
     forSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
            withReuseIdentifier:HEADER_ID];
    

    that ALSO in the nib file, when you select the top-level collection reusable view, use the attributes inspector, and make sure the Identifier is set to the same value you are passing in to the withReuseIdentifier: parameter.

    0 讨论(0)
  • 2020-12-17 08:57

    Replace

    NSString *CellIdentifier = @"Cell";
    

    with

    static NSString *CellIdentifier = @"Cell";
    
    0 讨论(0)
  • 2020-12-17 09:00

    I got this crash on iOS 9 only (iOS 10/11 are working fine).

    I had no custom subclass of a Flow Layout but setting the headerReferenceSize on the existing one directly. So in Interface Builder with Section Header enabled I got this crash, without the checkmark everything works fine and the headers are being displayed correctly, since I set the size in code.

    0 讨论(0)
  • 2020-12-17 09:04

    You need to register like below:

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MY_CELL"];
    
    0 讨论(0)
  • 2020-12-17 09:04

    I had the same problem. Here's how I solved it.

    Move

    [self.pictureCollectionView registerNib:[UINib nibWithNibName: bundle:nil] forCellWithReuseIdentifier:reuseID]

    to be in - (void)viewDidLoad,

    rather than method - (void)awakeFromNib.

    0 讨论(0)
提交回复
热议问题