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

编程入门 行业动态 更新时间:2024-10-22 04:57:56
本文介绍了'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':尝试滚动到无效的索引路径

本文发布于:2023-11-26 17:13:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   索引   NSInvalidArgumentException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!