ios - Collection View 完成处理程序的 PerformBatchUpdates 未执行

标签 ios objective-c swift iphone mobile

我目前遇到的有趣问题。在 UICollectionView 实例上调用 PerformBatchUpdates 方法时,某些情况会导致完成处理程序无法执行。

collectionView.performBatchUpdates({
            collectionView.insertItems(at: indexPaths)
        }) { _ in
            if self.delegate.responds(to: #selector(self.delegate.sectionNeedsToBeHidden)) {
                self.delegate.sectionNeedsToBeHidden?()
            }
        }
    }

当向 Collection View 执行插入操作时,不会调用完成处理程序。但是,当执行相同的逻辑时,除了将 collectionView.insertItems(at: indexPaths) 行更改为 collectionView.deleteItems(at: indexPaths) 时,将调用完成处理程序。我目前对发生这种情况的猜测与 Collection View 的先前状态有关,因此它只是在删除时注册对数据源的更改,从而导致调用完成处理程序。然而,该解决方案并不确定为什么仅在删除项目而不是插入项目时调用 performBatchUpdatescompletionHandler。如果有人对这个确切的问题或一般的 performBatchUpdates 工作方式有任何见解,我将不胜感激

最佳答案

每当您插入一行或从 Collection View 中删除一个项目时,您都必须更新许多项目数据,如下面的代码

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.myItems.count
}

self.myItems.count 需要先更新。因此,首先更新您的项目计数,然后执行批量更新。

关于ios - Collection View 完成处理程序的 PerformBatchUpdates 未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59062641/

相关文章:

ios - Twitter 获取请求不断返回代码 88 - "Rate limit exceeded"

objective-c - 从 url 加载 .plist 文件

ios - 更改整个应用程序的键盘外观

ios - 使用AVFoundation框架进行视频录制

ios - 使用 RxSwift 阻塞线程

ios - 使用 NSArray 和 NSDictionary : Can not convert value of type "string" to expected argument type "int"

objective-c - iOS:Vimeo 视频播放器

android - 在Cordova中基于解析电话的登录

ios - 如何仅在 Debug模式下启用3D触摸(静态快速操作)?

ios - 如何在透视中绘制图像?