Error could not dequeue a view of kind UICollectionElementKindCell

前端 未结 13 1226
耶瑟儿~
耶瑟儿~ 2020-12-16 09:34

Taking first plunge with collection views and am running into this error:

Terminating app due to uncaught exception \'NSInternalInconsistencyException

相关标签:
13条回答
  • 2020-12-16 09:56

    i had everything 100% done correctly. but for some reason i got the same error for an hour, then what i did is:

    1. go to storyboard
    2. select the prototype cell (in my case)
    3. clear the class name from identity inspector, then rewrite it
    4. rewrite the identifier name in the attributes inspector

    simply, redid this step even tho i made it correct before, its like xcode misses something at a specific nanosecond!

    0 讨论(0)
  • 2020-12-16 09:57

    You need to use same identifier between the dequeueReusableCellWithReuseIdentifier's argument and the UICollectionViewCell's property.

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    

    identifier

    0 讨论(0)
  • 2020-12-16 10:02

    You need to give correct reuseble identifier in storyboard, which you have give in the code while registering the collectionViewCell.

    Here is the code.

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ClctnVC", for: indexPath)as! ClctnVC
        return cell            
    }
    
    0 讨论(0)
  • 2020-12-16 10:03

    Complementing what @jrtuton written... What you need is:

    1) Register your nib file to "connect" it with your identifier:

    //MyCollectionView.m
    - (void) awakeFromNib{
     [self registerNib:[UINib nibWithNibName:@"NibFileName" bundle:nil]   forCellWithReuseIdentifier: @"MyCellIdentifier"];
    }
    

    2) Use your identifier to load your custom cell from a nib:

    //MyCollectionView.m
    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath  *)indexPath {
        MyCustomCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"MyCellIdentifier" forIndexPath:indexPath];
    }
    

    3) Use always static NSString* to avoid the same identifiers again in your app.

    0 讨论(0)
  • 2020-12-16 10:03

    What fixed this for me was (roughly) same as Omar's answer, except I ended up creating a new UICollectionViewCell custom class and giving the Cell Reuse Indentifier a new / different name than the one that I had used elsewhere in the application. It worked immediately after that.

    0 讨论(0)
  • 2020-12-16 10:05

    Just in case, if you are working with storyboard set collectionView identifier in the right place in the Attributes Inspector -> Identifier field. Not under the class name in "Restoration ID".

    If you are using collection view in tableView cell, add delegates to tableView cell not in the tableViewController.

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