由于“UICollectionViewLayoutAttributes”而在集合视图单元格删除时崩溃

时间:2017-10-06 06:53:13

标签: ios swift uicollectionview uicollectionviewlayout

我的数据源是一个二维数组,因为我将表情符号分为几个部分。

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

中有这个
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
        }
    }

错误 -

  

***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'不   UICollectionViewLayoutAttributes实例   -layoutAttributesForSupplementaryElementOfKind:路径{length = 2,path = 0 - 0}'的

删除单元格时出现此错误。崩溃并不总是发生,它是随机的。

sectionCollectionView与UICollectionView完全不同。

0 个答案:

没有答案