Error could not dequeue a view of kind UICollectionElementKindCell

前端 未结 13 1364
耶瑟儿~
耶瑟儿~ 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 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.

提交回复
热议问题