'NSInvalidArgumentException' : attempt to scroll to invalid index path

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

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

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

Section Code:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{      return 1; } 

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

Thank You.

Best Regards.

回答1:

Keep this line inside an if:

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

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]; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!