7

2つのUICollectionViewで設定されたビューがあります。これらの各ビューには、さまざまなサイズの配列があります。collection1はarray1に支えられており、collection2はarray2に支えられています。問題は、numberOfItemsInSectionからcollection1に返される数値が、両方のコレクションビューに適用されていることです。

たとえば、array1がサイズ4で、array2がサイズ5の場合、両方のコレクションに4つの要素が表示されます。array1がサイズ5でarray2がサイズ4の場合、collection2をスクロールすると、collection2のitemIndexが5のcellForItemAtIndexPathが呼び出され、NSRangeExceptionが発生します。

各collectionViewに独自のサイズを使用させるにはどうすればよいですか?

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    if(view == self.colleciton1){
        return self.array1.count;
    } else if (view == self.collection2){
        return self.array2.count;
    }

    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    if(cv == self.collection1){
        CharacterCell *cell = [cv dequeueReusableCellWithReuseIdentifier:FIRST_CELL_IDENTIFIER forIndexPath:indexPath];
        cell.label.text = self.array1[indexPath.item];
        return cell;
    } else if (cv == self.collection2){
        EpisodeCell *cell = [cv dequeueReusableCellWithReuseIdentifier:SECOND_CELL_IDENTIFIER forIndexPath:indexPath];
        cell.label.text = self.array2[indexPath.item];
        return cell;
    }

    return nil;
}

問題を説明するプロジェクトにgitリポジトリを含めました。

git@github.com:civatrix / MultipleCollectionViews.git

4

3 に答える 3

20

問題は、各コレクションに同じレイアウトオブジェクトを使用していたことでした。振り返ってみると、それは理にかなっていますが、collectionViewごとに異なるレイアウトを作成する必要があります。

于 2012-10-12T15:05:08.533 に答える
5

おそらく、ContainerViewsを使用し、UICollectionViewごとに2つの個別のUICollectionViewコントローラーを使用する方が簡単です。

于 2013-01-07T21:53:20.540 に答える
1

あなたが持っているものはうまくいくはずです。self.colleciton1とself.collection2はIBOutletsですか?もしそうなら、それらが正しく接続されていることを再確認できますか?

于 2012-10-12T01:07:23.563 に答える