【发布时间】:2012-11-16 15:38:34
【问题描述】:
我在 UICollectionView 上遇到自定义布局对象的问题。插入项目时,会抛出NSInternalInconsistencyException:
2012-11-16 10:01:18.920 MyApp[4520:430b] * -[NSIndexPath 行] 中的断言失败,/SourceCache/UIKit/UIKit-2372/UITableViewSupport.m:2680 2012-11-16 10:01:18.924 MyApp[4520:430b] 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“用于 UITableView 的索引路径无效。传递给表视图的索引路径必须恰好包含两个指定节和行的索引。如果可能,请使用 UITableView.h 中 NSIndexPath 上的类别。 ** 首先抛出调用堆栈: (0x370282a3 0x33eb097f 0x3702815d 0x347d72af 0x338c29bb 0x33cb8e1f 0x33ca95db 0x9cff9 0x33ca44b5 0x33ca5e73 0x33ca4295 0x33ca69d9 0x9a0a9 0xa554f 0x34797539 0x3478edb9 0x348073db 0x360cb11f 0x360cf961 0x360cfac1 0x36dd3a11 0x36dd38a4) libc++abi.dylib:终止调用抛出异常
下面是开始插入的代码:
GetItemsRequest *request = [[GetItemsRequest alloc] init];
self.activeConnection = [[ServiceConnection alloc] initWithRequest:request
completion:^(GetItemsResponse *response, NSError *error) {
[self.collectionView performBatchUpdates:^{
[self.items addObjectsFromArray:response.items];
NSMutableArray *newIndexPaths = [NSMutableArray array];
for (Item *item in response.items)
{
[newIndexPaths addObject:[NSIndexPath indexPathForItem:[self.items indexOfObject:item]
inSection:0]];
}
[self.collectionView insertItemsAtIndexPaths:newIndexPaths];
} completion:NULL];
[self.pullRefreshView refreshLastUpdatedDate];
[self.pullRefreshView scrollViewDataSourceDidFinishedLoading:self.collectionView];
self.activeConnection = nil;
}];
[self.activeConnection start];
如果我用UICollectionViewFlowLayout 对象替换我的自定义布局对象,则此代码有效,所以我认为它是正确的。如果我简单地调用[self.collectionView reloadData],自定义布局就可以工作,所以布局对象处理插入的方式肯定有问题。
崩溃发生在对super的调用中:
- (void)prepareForCollectionViewUpdates:(NSArray *)updateItems
{
[super prepareForCollectionViewUpdates:updateItems];
for (UICollectionViewUpdateItem *item in updateItems)
{
if (item.updateAction == UICollectionUpdateActionDelete)
{
_items--;
}
else if (item.updateAction == UICollectionUpdateActionInsert)
{
_items++;
}
}
_rows = -1;
}
然而,当我打印updateItems 的描述时,所有的索引路径实际上都有两个索引:
<__NSArrayM 0x1f06af20>( <UICollectionViewUpdateItem: 0x1ddc2360> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc1eb0> 2 indexes [0, 0]) action (insert), <UICollectionViewUpdateItem: 0x1ddc23d0> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc1f00> 2 indexes [0, 1]) action (insert), <UICollectionViewUpdateItem: 0x1ddc23f0> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc1f20> 2 indexes [0, 2]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2410> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc1f50> 2 indexes [0, 3]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2430> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc1f80> 2 indexes [0, 4]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2470> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc1e00> 2 indexes [0, 5]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2490> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc1e20> 2 indexes [0, 6]) action (insert), <UICollectionViewUpdateItem: 0x1ddc24b0> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc1fe0> 2 indexes [0, 7]) action (insert), <UICollectionViewUpdateItem: 0x1ddc24d0> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc2010> 2 indexes [0, 8]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2450> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc1fb0> 2 indexes [0, 9]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2530> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc2090> 2 indexes [0, 10]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2550> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc20c0> 2 indexes [0, 11]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2570> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc20f0> 2 indexes [0, 12]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2590> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc2120> 2 indexes [0, 13]) action (insert), <UICollectionViewUpdateItem: 0x1ddc25b0> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc2150> 2 indexes [0, 14]) action (insert), <UICollectionViewUpdateItem: 0x1ddc25d0> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc2180> 2 indexes [0, 15]) action (insert), <UICollectionViewUpdateItem: 0x1ddc25f0> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc21b0> 2 indexes [0, 16]) action (insert), <UICollectionViewUpdateItem: 0x1ddc2680> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc2250> 2 indexes [0, 17]) action (insert), <UICollectionViewUpdateItem: 0x1ddc26a0> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc2280> 2 indexes [0, 18]) action (insert), <UICollectionViewUpdateItem: 0x1ddc26c0> index path before update ((null)) index path after update (<NSIndexPath 0x1ddc22b0> 2 indexes [0, 19]) action (insert) )
那么为什么这段代码会抛出一个异常说正好需要两个索引呢?任何帮助表示赞赏。
【问题讨论】:
-
我自己还在积极调查中,所以我会继续更新这个帖子:当批量更新块被调用时,布局方法被依次调用,
layoutElementsForAttributesInRect:,prepareLayout,和prepareForCollectionViewUpdates:。在调用第一个方法时,[self.collectionView numberOfItemsInSection:0]返回 0,因此该方法不返回任何属性。也许这是相关的?
标签: objective-c ios ios6 uicollectionview