2017-04-20 46 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 
相關問題