2

これは 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;
}

これは私にとって新しいことなので、助けてください。

ありがとうございました。

よろしくお願いします。

4

1 に答える 1

5

この行をif:内に保持します。

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

セクション カウント 1 を渡しているため、インデックスは 0 ベースであるため、セクション 0 を 1 ではなく保持してください。numberOfSectionsInCollectionView = 2 の場合、2 つのセクションを持つことができます{0, 1}

[self.offerCollectionView scrollToItemAtIndexPath: [NSIndexPath indexPathForItem: rowIndex inSection: 0];
                  atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                          animated:YES];
于 2013-08-13T06:18:24.180 に答える