简体   繁体   English

集合视图完成处理程序的PerformBatchUpdates未执行

[英]PerformBatchUpdates for collection view completion handler not being executed

Interesting problem I am running into currently. 我目前遇到的有趣问题。 When invoking the performBatchUpdates method on an instance of a UICollectionView certain cases cause the completion handler to not be executed. 在某些情况下,在UICollectionView的实例上调用performBatchUpdates方法时,将导致不执行完成处理程序。

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

When performing an insertion to the collection view the completion handler is not invoked. 在对collection view执行插入时,不会调用完成处理程序。 However, when the same logic is executed except the line collectionView.insertItems(at: indexPaths) is changed to collectionView.deleteItems(at: indexPaths) the completion handler is then invoked. 但是,当执行相同的逻辑(除了将collectionView.insertItems(at: indexPaths)更改为collectionView.deleteItems(at: indexPaths) ,则调用完成处理程序。 My current guess as to why this is happening is something regarding the previous state of the collection view and it is only registering a change to the data source upon deletion therefore, causing the completion handler to be invoked. 我目前对发生这种情况的原因的猜测是关于收集视图的先前状态的,因此它仅在删除后将更改注册到数据源,从而导致调用完成处理程序。 That solution however, is not definitive as to why the completionHandler for performBatchUpdates is only being invoked upon the deletion of items and not the insertion of items. 然而,这种解决方案是不明确的,为什么completionHandlerperformBatchUpdates只被在项目的删除调用,而不是项目的插入。 If anyone has any insights on this exact problem or in general of how the performBatchUpdates work I would appreciate 如果有人对这个确切的问题有任何见解,或者总体上对performBatchUpdates工作原理有任何见解,我将不胜感激

Whenever you insert a row or delete an item from collection view you must have to update a number of items data like below code 每当您插入行或从集合视图中删除项目时,都必须更新一些项目数据,如下面的代码

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

self.myItems.count need to update first. self.myItems.count需要首先更新。 So First update your items count then perform batch update. 因此,首先更新您的项目计数,然后执行批量更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用完成处理程序后执行的代码 - Code being executed after completion handler is called 当涉及部分操作时,不会调用 performBatchUpdates 完成处理程序 - performBatchUpdates completion handler is not called when there is section operation involved 如果UITableViewController在performBatchUpdates完成处理程序中捕获自身,这会导致保留周期吗? - If a UITableViewController captures self in a performBatchUpdates completion handler, can that cause a retain cycle? 即使在完成处理程序中,Swift代码也将异步执行 - Swift code being executed asynchronously even while in completion handler iOS 11 UITableView performBatchUpdates未调用完成闭包 - iOS 11 UITableView performBatchUpdates completion closure not being called 完成处理程序已执行,但模态视图控制器仍在屏幕上并已引用 - Completion Handler is Executed, but Modal View Controller is Still On Screen and Referenced performBatchUpdates中的reloadData:completion:不调用cellForItemAtIndexPath: - reloadData in performBatchUpdates:completion: does not call cellForItemAtIndexPath: 关闭屏幕时不调用UICollectionView performBatchUpdates完成 - UICollectionView performBatchUpdates completion not called while off screen 未为 ASWebAuthenticationSession 调用会话完成处理程序 - Session completion handler not being called for ASWebAuthenticationSession 弹出视图时,完成处理程序会如何处理? - What happens to a completion handler when a view is popped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM