0

I am experiencing a problem with a custom layout object on a UICollectionView. When inserting items, an NSInternalInconsistencyException is thrown:

2012-11-16 10:01:18.920 MyApp[4520:430b] * Assertion failure in -[NSIndexPath row], /SourceCache/UIKit/UIKit-2372/UITableViewSupport.m:2680 2012-11-16 10:01:18.924 MyApp[4520:430b] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.' ** First throw call stack: (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 called throwing an exception

Here is the code that starts the insert:

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

This code works if I replace my custom layout object with a UICollectionViewFlowLayout object, so I think it is correct. The custom layout works if I simply call [self.collectionView reloadData], so there must be something wrong with how the layout object handles inserts.

The crash occurs in the call to 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;
}

Yet, when I print the description of updateItems, all of the index path do, in fact, have 2 indexes:

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

So why would this code throw an exception saying that exactly two indexes are required? Any help is appreciated.

4

1 回答 1

1

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

于 2012-11-16T17:11:06.327 回答