UICollectionView insertItemsAtIndexPaths抛出NSInternalInconsistencyException

时间:2012-11-16 15:38:34

标签: objective-c ios ios6 uicollectionview

我在UICollectionView上遇到自定义布局对象的问题。插入项目时,会抛出NSInternalInconsistencyException

  

2012-11-16 10:01:18.920 MyApp [4520:430b] *断言失败 - [NSIndexPath row],/ 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:terminate调用抛出异常

以下是启动插入的代码:

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的描述时,所有索引路径实际上都有2个索引:

  

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

那么为什么这段代码会抛出一个异常,说只需要两个索引?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

知道了。在为 headers 创建布局属性时,我使用[NSIndexPath indexPathWithIndex:]。由于布局对象决定补充视图的索引路径意味着什么,我以为我只能使用一个索引。显然不是。