2017-04-20 4 views
2

사진을 표시하는 데 사용 된 UICollectionView 사례가 있습니다. 이 컬렉션보기에는 사진 개수를 표시하는 추가보기 (머리글)가 있습니다.UICollectionView의 보완 뷰 (헤더)를 invalidationContext로 다시로드하는 중 문제가 발생했습니다.

내 비즈니스 로직은 콜렉션 뷰에서 어떤 시점에서 insertItems(at:) 또는 removeItems(at:)을 요구하므로 사진 개수를 업데이트하려면 보완 뷰를 다시로드해야합니다.

을 다시로드/새로 고침 할 수있는 일반 솔루션 (UICollectionView 확장이 유용 할 것입니다)을 찾고 있는데, 컬렉션보기의 추가보기 만입니다.

내가 원하지 않는 솔루션이 : 컬렉션 뷰의

  • 새로 고침 전체 섹션 컬렉션 뷰의
  • 새로 고침 데이터
  • 다시 그리기 또는 보조 뷰 이외의 다른를 다시로드

나는 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView을 트리거하는 무언가가 필요하며 보충 뷰를 다시 그립니다.

나는이 시도했다, 그러나 일반적인 NSException와 충돌 :

self.photosCollectionView.insertItems(at: [IndexPath(row: index, section:0)]) 

let context = UICollectionViewLayoutInvalidationContext() 
       context.invalidateSupplementaryElements(ofKind: UICollectionElementKindSectionHeader, at: [IndexPath(row: 0, section: 0)]) 

let layout = self.photosCollectionView.collectionViewLayout as? UICollectionViewFlowLayout 
       layout?.invalidateLayout(with: context) 

답변

1
당신의 항목 삽입 방법 내부

및 항목 방법을 삭제, 당신이 필요 없어 이런 식으로 헤더 뷰 레이블의 수를 지정 컬렉션보기를 다시로드하십시오.

let headerView = collectionViewDemo.supplementaryView(forElementKind: UICollectionElementKindSectionHeader, at: [0,0]) as? DemoCollectionReusableView 
      headerView?.textLabel.text = self.count