由于“UICollectionViewLayoutAttributes”而导致集合查看单元格删除时崩溃

我的数据源是一个二维数组,因为我将emojis分成了多个小节。

eg. [[a,b,c,d],[e,f,g],[h,j,k,l,m]] 

我的单元格删除代码 –

 func deleteEmoji(at indexPath: IndexPath) { // Get the bad emoji let emojiToBeDeleted = emojisArray[indexPath.section][indexPath.row] // Delete the emoji from datasource and collection view emojisArray[indexPath.section].remove(at: indexPath.row) stickerCollectionView.deleteItems(at: [indexPath]) // Delete the section from datasource and collection view as its emoji count is zero if emojisArray[indexPath.section].count == 0 { emojisArray.remove(at: indexPath.section) stickerCollectionView.deleteSections(IndexSet(integer: indexPath.section)) // Crashing here :( sectionCollectionView.reloadData() } // Delete the bad emoji emojiToBeDeleted.delete() // Undo the deletion mode as all emoji's are deleted if emojisArray.count == 0 { isDeletionMode = false } } 

除了最后一节,我还为每个部分都有脚注,因为我在UICollectionViewDataSource有这个UICollectionViewDataSource

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { switch kind { case UICollectionElementKindSectionFooter: let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Identifier.footer, for: indexPath) return headerView default: assert(false, "Unexpected element kind") } } 

并在UICollectionViewDelegate

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { switch collectionView { case stickerCollectionView: if section == emojisArray.count - 1 { return CGSize.zero } return CGSize(width: collectionView.bounds.size.width, height: 15) default: return CGSize.zero } } 

错误 –

***由于未捕获exception“NSInternalInconsistencyException”而终止应用程序,原因:'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionFooter at path {length = 2,path = 0 – 0}'

我在删除单元格时遇到此错误。 而崩溃并不总是发生,这是相当随机的。

sectionCollectionView是完全不同的UICollectionView。