gpt4 book ai didi

objective-c - UICollectionView insertItemsAtIndexPaths 抛出 NSInternalInconsistencyException

转载 作者:行者123 更新时间:2023-11-29 04:17:54 26 4
gpt4 key购买 nike

我在 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

这是启动插入的代码:

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

那么为什么这段代码会抛出异常,指出需要恰好两个索引?如有任何帮助,我们将不胜感激。

最佳答案

明白了。在为标题创建布局属性时,我使用了[NSIndexPath indexPathWithIndex:]。由于布局对象决定了补充 View 的索引路径的含义,因此我认为我可以只使用一个索引。显然不是。

关于objective-c - UICollectionView insertItemsAtIndexPaths 抛出 NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13419729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com