UICollectionView:如何在加载视图之前和之后处理更新? [英] UICollectionView: How to handle updates both before and after view is loaded?

查看:128
本文介绍了UICollectionView:如何在加载视图之前和之后处理更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过KVO接收收藏集视图时,我在更新收藏夹视图时遇到问题。



例如对于删除,我执行以下操作:

  dispatch_async(dispatch_get_main_queue(),^ {
NSUInteger index = [self.albumObjects indexOfObject:oldObject];
NSIndexPath * indexPath = [NSIndexPath indexPathForItem:index inSection:0];
[self.dataSource removeObjectAtIndex:index];
[self.collectionView deleteItemsAtIndexPaths:@ [indexPath] ];
}

上面的错误给了我:

 尝试在更新


好...所以让我们随后从数据源中删除,然后返回以下错误:

  dispatch_async(dispatch_get_main_queue(),^ {
NSUInteger index = [self.albumObjects indexOfObject:oldObject];
NSIndexPath * indexPath = [NSIndexPath indexPathForItem:index inSection:0];
[self.collectionView deleteItemsAtIndexPaths:@ [indexPath ]];
[self.dataSource removeObjectAtIndex:index];
}

但是我现在收到以下错误:

 无效更新:第0节中的无效项数。更新(1)之后现有节中包含的项数必须等于更新前该部分中包含的项目(1),加上或减去从该部分中插入或删除的项目数(已插入0,已删除1),以及加上或减去移入或移出该部分的项目数(已移动0)进入,0移出)。 

所以我似乎赢不了?我在这里做错了什么?

解决方案

看来,dispatch_async()会导致以下异常:当您修改数据源时(这会将updater块添加到主队列),则当您尝试更新collectionView时,该项目已被删除。



您使用什么数据源?它是手动管理的吗?还是viewController正在观察数据源?


I'm having issues updating a collection view when receiving those updates via KVO.

e.g. for deletion I perform the following:

dispatch_async(dispatch_get_main_queue(), ^{
    NSUInteger index = [self.albumObjects indexOfObject:oldObject];
    NSIndexPath* indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    [self.dataSource removeObjectAtIndex:index];
    [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
}

The above gives me the error:

attempt to delete item 0 from section 0 which only contains 0 items before the update

Ok... so lets remove from the data source afterwards then which then returns the following error:

dispatch_async(dispatch_get_main_queue(), ^{
    NSUInteger index = [self.albumObjects indexOfObject:oldObject];
    NSIndexPath* indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
    [self.dataSource removeObjectAtIndex:index];
}

However I now get the following error instead:

Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).

So it seems I can't win? What am I doing wrong here?

解决方案

It seems the dispatch_async() causes the exception: when you modify your datasource (which adds your updater block to the end of the main queue), the item is already deleted when you try to update your collectionView.

What dataSource do you use? Is it managed manually? Or the the viewController is observing the datasource?

这篇关于UICollectionView:如何在加载视图之前和之后处理更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆