Ios 从UICollectionView中的节中删除行不会';似乎无法删除关联的标题

Ios 从UICollectionView中的节中删除行不会';似乎无法删除关联的标题,ios,swift,header,uicollectionview,uicollectionviewlayout,Ios,Swift,Header,Uicollectionview,Uicollectionviewlayout,我有自己的自定义UICollectionViewLayout,它为给定节中的每一行添加一个自定义标题,当我从该节中删除一个项目时,会出现以下崩溃: ***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“路径{length=2,path=2-0}处的-LayoutAttributesForSupplmentalElementOfKind:UICollectionElementKindSectionHeader没有UICollectionV

我有自己的自定义UICollectionViewLayout,它为给定节中的每一行添加一个自定义标题,当我从该节中删除一个项目时,会出现以下崩溃:

***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“路径{length=2,path=2-0}处的-LayoutAttributesForSupplmentalElementOfKind:UICollectionElementKindSectionHeader没有UICollectionViewLayoutAttributes实例”

这对我来说毫无意义。我刚刚删除了2-0处的行,为什么它试图从我的布局类请求一个标题

这是我删除行的代码:

collectionView?.performBatchUpdates({
            self.trackControllers.remove(at: index)
            if self.collectionView?.numberOfItems(inSection: 2) > index {
                self.collectionView?.deleteItems(at: [IndexPath(row: index, section: 2)])
            }
        })

不管删除后节是否为空,或者是否还有其他行,它似乎仍然会请求我刚才删除的行的标题。

因此我自己设法在这里解决了这个问题

显然,UICollectionView将继续请求相同的补充视图,直到删除动画结束。为了解决这个问题,我必须重写
IndexPath-stodeleteforsupplementaryview(ofKind-elementKind:String)->[IndexPath]
。现在,这很愚蠢地没有为我们提供任何IndExpath已被删除的信息,因此您必须自己在
prepare(ForCollectionViewUpdateItems:[UICollectionViewUpdateItem])中跟踪这一点。

以下是修复我的问题的代码:

fileprivate var deletedIndexPaths = [IndexPath]()
override func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]) {
    deletedIndexPaths.removeAll()
    super.prepare(forCollectionViewUpdates: updateItems)

    let newlyDeletedItemsInSection2 = updateItems.filter({ $0.updateAction == .delete }).filter({ $0.indexPathBeforeUpdate?.section == 2 })
    let newlyDeletedIndexPaths = newlyDeletedItemsInSection2.flatMap({ $0.indexPathBeforeUpdate })
    deletedIndexPaths.append(contentsOf: newlyDeletedIndexPaths)
}

override func indexPathsToDeleteForSupplementaryView(ofKind elementKind: String) -> [IndexPath] {
    return deletedIndexPaths
}

override func finalizeCollectionViewUpdates() {
    deletedIndexPaths.removeAll()
}

(请注意,我仅有的章节有标题是第2节)

因此我在这里设法自己解决了这个问题

显然,UICollectionView将继续请求相同的补充视图,直到删除动画结束。为了解决这个问题,我必须重写
IndexPath-stodeleteforsupplementaryview(ofKind-elementKind:String)->[IndexPath]
。现在,这很愚蠢地没有为我们提供任何IndExpath已被删除的信息,因此您必须自己在
prepare(ForCollectionViewUpdateItems:[UICollectionViewUpdateItem])中跟踪这一点。

以下是修复我的问题的代码:

fileprivate var deletedIndexPaths = [IndexPath]()
override func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]) {
    deletedIndexPaths.removeAll()
    super.prepare(forCollectionViewUpdates: updateItems)

    let newlyDeletedItemsInSection2 = updateItems.filter({ $0.updateAction == .delete }).filter({ $0.indexPathBeforeUpdate?.section == 2 })
    let newlyDeletedIndexPaths = newlyDeletedItemsInSection2.flatMap({ $0.indexPathBeforeUpdate })
    deletedIndexPaths.append(contentsOf: newlyDeletedIndexPaths)
}

override func indexPathsToDeleteForSupplementaryView(ofKind elementKind: String) -> [IndexPath] {
    return deletedIndexPaths
}

override func finalizeCollectionViewUpdates() {
    deletedIndexPaths.removeAll()
}
(请注意,我仅有的部分有标题是第2部分)

感谢您对“indexPathsToDeleteForSupplementaryView”的提示。引用文档“例如,删除节时,您可能希望删除与该节关联的补充视图。”删除节时,可能会删除节标题。我可能会…感谢您对“IndExpathStodeleteForSupplementView”的提示。引用文档“例如,删除节时,您可能希望删除与该节关联的补充视图。”删除节时,可能会删除节标题。我可能。。。