'NSInvalidArgumentException':尝试滚动到无效的索引路径 [英] 'NSInvalidArgumentException' : attempt to scroll to invalid index path

查看:593
本文介绍了'NSInvalidArgumentException':尝试滚动到无效的索引路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我为CollectionViewCustomCells设置动画的代码。

This is my code for animating the CollectionViewCustomCells.

-(void)viewDidAppear:(BOOL)animated{

    rowIndex = 0;

    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(targetMethod)
                                   userInfo:nil
                                    repeats:YES];
}

-(void)targetMethod
{
    [self.offerCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:rowIndex inSection:1]
                      atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                              animated:YES];

    rowIndex=(rowIndex<parserDataContentArrayForExhibitor.count-1)?(rowIndex+1):0;

//    if (rowIndex == parserDataContentArrayForExhibitor.count) {
//        rowIndex = 0;
//    }


}


Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: <NSIndexPath 0xab55110> 2 indexes [1, 0]'

每次运行我的应用程序时都会遇到此异常。 但是有时应用程序运行良好。
我相信有些泄漏或某些东西。
我尝试了什么:当我用Google搜索时,发现这种错误是因为如果我写了一个不存在的节,并且该节的索引对于第一节来说是0。

I am getting this exception each time i run my app. But sometimes the app is running good. I believe there is some leak or something kind of stuff. What I tried : When i googled I Found that this kind of error is because if i write a section which does not exist and the section's index is 0 for the first section.

所以我将目标方法更改为:

so i changed my target method to :

-(void)targetMethod
{
    [self.offerCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:rowIndex inSection:0]
                      atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                              animated:YES];

    rowIndex=(rowIndex<parserDataContentArrayForExhibitor.count-1)?(rowIndex+1):0;

//    if (rowIndex == parserDataContentArrayForExhibitor.count) {
//        rowIndex = 0;
//    }


}

但是然后在异常参数稍有变化的情况下也会发生同样的事情。

But then the same thing happens with a slight change in the parameter of the exception.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: <NSIndexPath 0xab22ea0> 2 indexes [0, 0]'

段代码:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

请帮我,因为这对我来说是新的。

PLease help me as this is kind of new to me.

谢谢。

最好的问候。

推荐答案

将此行保留在 if 中:

if (parserDataContentArrayForExhibitor.count > 0) {
 rowIndex = (rowIndex<parserDataContentArrayForExhibitor.count - 1) ? (rowIndex + 1) : 0;
}

由于您传递的段数为1,因此请保持段0而不是1,因为索引是从零开始的。如果您的numberOfSectionsInCollectionView = 2,则可以有两个部分 {0,1}

Since you are passing a section count of 1, keep your section 0 and not 1 because the indexing is zero based. If you had numberOfSectionsInCollectionView = 2 then you could have two sections {0, 1}

[self.offerCollectionView scrollToItemAtIndexPath: [NSIndexPath indexPathForItem: rowIndex inSection: 0];
                  atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                          animated:YES];

这篇关于'NSInvalidArgumentException':尝试滚动到无效的索引路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆