首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionView insertItemsAtIndexPaths Throws

UICollectionView insertItemsAtIndexPaths Throws
EN

Stack Overflow用户
提问于 2012-11-16 15:38:34
回答 1查看 2.5K关注 0票数 0

我在UICollectionView上遇到了自定义布局对象的问题。插入项时,将引发NSInternalInconsistencyException

2012年-11-16 10:01:18.920 MyApp4520:430 b * -NSIndexPath行断言失败,-NSIndexPath 2012-11-16 10:01:18.924 MyApp4520:430 b终止应用程序,原因是:“与UITableView一起使用的无效索引路径”。传递给表视图的索引路径必须包含两个指定节和行的索引。如果可能,请在UITableView.h中使用NSIndexPath上的类别。**第一次抛出调用堆栈:(0x370282a3 0x33eb097f 0x3702815d 0x347d72af 0x338c29bb 0x33cb8e1f 0x33ca95db 0x9cff9 0x33ca44b5 0x33ca4295 0x332990x9a9 0xa9 0xa554f 0x797539 0x3478edb9 0x3480db 0x60cb11f 0x60cf6x63x3facc1 36dd3x11 36ddx0x4 libc++abi.dylib),称为抛出异常。

下面是启动插入的代码:

代码语言:javascript
复制
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的调用中。

代码语言:javascript
复制
- (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) )

那么,为什么这段代码会抛出一个异常,说明确实需要两个索引?任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-16 17:11:06

明白了。在为标题创建布局属性时,我使用了[NSIndexPath indexPathWithIndex:]。因为布局对象决定了补充视图的索引路径意味着什么,我想我只需要一个索引就可以了。显然不是。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13419729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档