【问题标题】:Reload collectionview cells and not the section header重新加载collectionview单元格而不是节标题
【发布时间】:2018-01-20 00:31:32
【问题描述】:

当尝试创建可折叠的 UICollectionView 部分时,我会根据其状态更新该部分中的项目数。但是,这样做,我重新加载了部分,这也重新加载了部分标题,当在部分标题中为我的图像设置动画时,我得到了一个非常奇怪的行为。

本质上,在更改部分项目时重新加载部分标题可以使 UICollectionView 更新项目,但部分动画看起来和行为很奇怪。 在不调用 reloadSection 的情况下,它允许正确的动画,但项目不会加载。

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

有什么办法解决这个问题?

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    您可以尝试在特定部分提取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)
    }
    

    【讨论】:

    • 我在 reloadSection 中这样做:self?.collectionView?.performBatchUpdates({ let indexSet = IndexSet(integer: section) self?.collectionView?.reloadSections(indexSet) }, completion: nil)跨度>
    • 我做到了。它给了我以前遇到的错误。无效更新:第 1 节中的项目数无效。更新后现有节中包含的项目数 (3) 必须等于更新前该节中包含的项目数 (0),加上或减去数字从该部分插入或删除的项目数(0 插入,0 删除)加上或减去移入或移出该部分的项目数(0 移入,0 移出)。
    • 这是另一个问题,从你的控制台我看到你正在修改集合视图,我想你应该用所涉及的完整代码来澄清这个问题。无论如何,应该在performBatchUpdates完成时调用reloadItems(在collection view做同步之后)
    • 我正在这样管理我的项目,这不正确吗? func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if let item = items[section] as? FactViewModelItem{ if item.type != .profileHeader { if item.isCollapsible && item.isCollapsed { return 0 } } } return items[section].rowCount }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多