UICollectionView:如何之前和之后查看处理更新加载?(UICollectionView

2019-09-26 17:14发布

我在通过志愿接收这些更新时更新集合视图的问题。

例如要删除我执行以下操作:

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]];
}

上面给我的错误:

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

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

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];
}

但是我现在碰到下面的错误,而不是:

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).

如此看来,我不能赢? 我在做什么错在这里?

Answer 1:

看来dispatch_async()导致异常:当你修改你的数据源(它增加了你的更新块到主队列的末尾),当您尝试更新您的CollectionView该项目已被删除。

你用什么数据源? 难道是手工管理? 或将是的viewController观测数据源?



文章来源: UICollectionView: How to handle updates both before and after view is loaded?