gpt4 book ai didi

ios - 'NSInvalidArgumentException' : attempt to scroll to invalid index path

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:29:59 25 4
gpt4 key购买 nike

这是我为 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]'

我每次运行我的应用程序时都会收到此异常。 但有时应用程序运行良好。我相信有一些泄漏或类似的东西。我尝试了什么:当我用谷歌搜索时,我发现这种错误是因为如果我写了一个不存在的部分并且该部分的第一部分索引为 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;
// }


}

但是同样的事情发生在异常参数的轻微变化上。

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 中:

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

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

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

关于ios - 'NSInvalidArgumentException' : attempt to scroll to invalid index path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201409/

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