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]]; }
以上给我的错误:
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).
所以看来我赢不了? 我在这里做错了什么?
看来dispatch_async()会导致exception:当你修改你的数据源(它将你的更新器块添加到主队列的末尾)时,当你尝试更新你的collectionView时,这个项目已经被删除了。
你用什么dataSource? 是否手动pipe理? 或者viewController正在观察数据源?
- UITableView上的animationreloadData
- 如何删除两个UICollectionView列之间的余量
- 在ScrollView中旋转缩放ImageView?
- UIColor colorWithRed:green:blue:alpha:总是返回白色,除非一个参数是0
- IOS在PDF中embedded字体
- UINavigationController过渡animation触发得太快
- 绘制UIImage时无效的上下文错误
- UIKeyboard避免和自动布局
- 从UICollectionView的viewForSupplementaryElementOfKind获取NSFetchedResultsController节对象