关于C#:UICollectionView insertItemsAtIndexPaths引发NSInternalInconsistencyException

UICollectionView insertItemsAtIndexPaths Throws NSInternalInconsistencyException

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

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

这是开始插入的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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的调用中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (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>(
index path before update ((null)) index path after update ( 2 indexes [0, 0]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 1]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 2]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 3]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 4]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 5]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 6]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 7]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 8]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 9]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 10]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 11]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 12]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 13]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 14]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 15]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 16]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 17]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 18]) action (insert),
index path before update ((null)) index path after update ( 2 indexes [0, 19]) action (insert)
)

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


得到它了。 创建标题的布局属性时,我使用的是[NSIndexPath indexPathWithIndex:]。 由于布局对象决定了补充视图的索引路径的含义,所以我认为我可以只使用一个索引。 显然不是。