2016-12-06 5 views
0

해당 섹션의 항목을 삭제할 때 해당 섹션의 각 행에 대한 사용자 지정 헤더를 추가하는 사용자 지정 UICollectionViewLayout이 있습니다. 다음 충돌 :UICollectionView의 섹션에서 행을 삭제해도 연결된 머리글이 삭제되지 않는 것 같습니다.

*** 인해 캐치되지 않는 예외 'NSInternalInconsistencyException'응용 프로그램 종료, 이유는 : '-layoutAttributesForSupplementaryElementOfKind 없음 UICollectionViewLayoutAttributes 인스턴스 : 경로에서 UICollectionElementKindSectionHeader {길이 = 2, 경로 = 2-0}'

나에게 의미가 없습니다. 방금 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)]) 
      } 
     }) 

을하거나, 아직의 헤더를 요청하는 것 방금 삭제 한 행.

답변

3

그래서 나는 여기 이것을 스스로 알아낼 수있었습니다.

분명히 UICollectionView는 삭제 애니메이션이 끝날 때까지 동일한 보완 뷰를 계속 요청합니다. 이 문제를 해결하려면 indexPathsToDeleteForSupplementaryView(ofKind elementKind: String) -> [IndexPath]을 무시해야했습니다.

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() 
} 

(참고 : 이제이 바보 indexPaths가 삭제 된있는 모든 정보를 제공하지 않습니다, 그래서 당신은 prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem])

이 자신을 추적 할 필요가 여기 내 문제를 해결 코드입니다 헤더가있는 섹션은 섹션 2)

+0

"indexPathsToDeleteForSupplementaryView"에 대한 힌트를 제공해 주셔서 감사합니다. 설명서 인용 "예를 들어, 섹션이 제거되면 해당 섹션과 관련된 추가보기를 제거 할 수 있습니다." 섹션을 제거하면 섹션 헤더가 제거 될 수 있습니다. 나는 ... –