簡體   English   中英

重新加載collectionview單元格而不是節標題

[英]Reload collectionview cells and not the section header

在嘗試創建可折疊的UICollectionView部分時,我會根據其狀態更新部分中的項目數。 但是,這樣做,我重新加載也重新加載節標題的部分,並且當在節標題中動畫我的圖像時,我得到一個非常奇怪的行為。

本質上,在更改節項時重新加載節標題使UICollectionView能夠更新項目,但是動畫部分看起來很奇怪。 不調用reloadSection,它允許正確的動畫但不加載項目。

self?.collectionView?.performBatchUpdates({ 
   let indexSet = IndexSet(integer: section)
   self?.collectionView?.reloadSections(indexSet) 
}, completion: nil)

有什么辦法解決這個問題?

您可以嘗試在特定部分中提取IndexPath序列,然后在此類序列上調用reloadItems ,這樣做:

extension UICollectionView {
   func reloadItems(inSection section:Int) {
      reloadItems(at: (0..<numberOfItems(inSection: section)).map {
         IndexPath(item: $0, section: section)
      })
   }
}

所以你的代碼可能是這樣的:

var updateSection = 0 // whatever
collectionView.performBatchUpdates({
    // modify here the collection view
    // eg. with: collectionView.insertItems
    // or: collectionView.deleteItems
}) { (success) in
    collectionView.reloadItems(inSection: updateSection)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM