繁体   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