'NSInvalidArgumentException':尝试滚动到无效的索引path

这是我的animationCollectionViewCustomCells的代码。

-(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]' 

每当我运行我的应用程序,我得到这个exception。 但有时候,应用程序运行良好。 我相信有一些泄漏或某种东西。 我试过的:当我GOOGLE了我发现,这种错误是因为如果我写一个部分不存在和部分的索引为0的第一节。

所以我改变了我的目标方法:

 -(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; // } } 

但是,同样的事情发生在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; } 

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

谢谢。

最好的祝福。

保持这条线的条件

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

因为ur传递节count = 1所以保持你的部分0不是1,因为索引从0开始不是1.如果你有numberOfSectionsInCollectionView = 2那么你可以有两个部分{0,1}

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