Reputation:
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.
Upvotes: 2
Views: 14980
Reputation: 12641
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];
Upvotes: 5