[英]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. 然而,这种解决方案是不明确的,为什么
completionHandler
为performBatchUpdates
只被在项目的删除调用,而不是项目的插入。 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.