【问题标题】:Changing collection view data source on tap随时更改集合视图数据源
【发布时间】:2016-02-02 14:38:42
【问题描述】:

我有一个集合视图,其中的项目数量逐渐增加。最初可能显示 100 个项目。然后,基于选择一个,我想在最后一个当前可见的项目之后插入项目。我还想刷新已经添加但我还没有看到的内容。

performBatchUpdates 做我需要它做的事情,但如果你快速选择/取消选择多个项目,性能会逐渐下降。

这是迄今为止我每次点击单元格时的内容:

performBatchUpdates({ () -> Void in
    collectionView.reloadItemsAtIndexPaths(reloadIndexPaths)
    collectionView.insertItemsAtIndexPaths(insertIndexPaths)
}, completion: { (completed: Bool) -> Void in

对我可以做些什么来平滑性能有什么想法吗? Instruments 验证了我的假设,即这是值得关注的领域。

【问题讨论】:

    标签: swift performance uicollectionview xcode-instruments


    【解决方案1】:

    尝试像这样async 这样做:

     dispatch_async(dispatch_get_main_queue()) { () in
             performBatchUpdates({ () -> Void in
               collectionView.reloadItemsAtIndexPaths(reloadIndexPaths)
               collectionView.insertItemsAtIndexPaths(insertIndexPaths)
              }, completion: { (completed: Bool) -> Void in
              ...
          }
    

    【讨论】:

    • 我忘了说整个事情已经被包裹在 dispatch_async 中了,这就是为什么我有点不确定什么是最好的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多