Error could not dequeue a view of kind UICollectionElementKindCell

前端 未结 13 1341
耶瑟儿~
耶瑟儿~ 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:18

    If you are using storyboard instead of xib, here are few steps.

    1. Making sure you fill the right identifier of your cell.

    1. In the ColletionViewDelegate.

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCellName", for: indexPath)as! YourCellName
          return cell            
      }
      
    2. That's it. You also don't want to add registerClass:forCellWithReuseIdentifier: which will cause element nil error.

提交回复
热议问题