简体   繁体   English

重新加载collectionview单元格而不是节标题

[英]Reload collectionview cells and not the section header

When trying to create collapsible UICollectionView sections, I update the number of items in the section dependent on its state. 在尝试创建可折叠的UICollectionView部分时,我会根据其状态更新部分中的项目数。 However, doing it this way, I reload the section which also reloads the section header aswell, and I get a very weird behavior when animating my image in the section header. 但是,这样做,我重新加载也重新加载节标题的部分,并且当在节标题中动画我的图像时,我得到一个非常奇怪的行为。

Essentially, reloading the section header when changing section items enables the UICollectionView to update the items but the section animate looks and behaves strange. 本质上,在更改节项时重新加载节标题使UICollectionView能够更新项目,但是动画部分看起来很奇怪。 Without calling reloadSection, it allows for the proper animation but the items do not load. 不调用reloadSection,它允许正确的动画但不加载项目。

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

What is the fix for this? 有什么办法解决这个问题?

You may try to extract the sequence of IndexPath in a specific section, then call reloadItems on such sequence, doing so: 您可以尝试在特定部分中提取IndexPath序列,然后在此类序列上调用reloadItems ,这样做:

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

so your code might be something like: 所以你的代码可能是这样的:

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