3

数時間解決できない問題があります...

セル数とセルサイズが異なる複数の UICollectionView があります。CollectionViews はプログラムで作成され、デリゲートとデータソースが設定されます。

コレクションビューは次のように作成されます。

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
collectionViewOne = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,320,150) collectionViewLayout:layout];
[collectionViewOne setTag:99];
[collectionViewOne setDataSource:self];
[collectionViewOne setDelegate:self];
[collectionViewOne registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[collectionViewOne setBackgroundColor:bgColor];
[collectionViewOne setShowsHorizontalScrollIndicator:NO];
[collectionViewOne setBounces:YES];
[collectionViewOne setAlwaysBounceHorizontal:YES];
[collectionViewOne setScrollEnabled:YES];
[collectionViewOne setRestorationIdentifier:@"collectionViewOne"];
[scrollView addSubview:collectionViewOne];

私の機能は次のようなものです:

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section {

    NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier);

    if (collectionView == collectionViewOne)
    {
        return 3;
    }
    else if (collectionView == collectionViewTwo)
    {
        return 4;
    }
    else
    {
        return 1;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier);

    if (collectionView == collectionViewOne)
    {
        return CGSizeMake(100, 150);
    }
    else if (collectionView == collectionViewTwo)
    {
        return CGSizeMake(200, 150);
    }
    else
    {
        return CGSizeMake(200, 150);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier);

    if (collectionView == collectionViewOne)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        imageView.image = [UIImage imageNamed:@"image.png"]; 
        [cell addSubview:imageView];
    }
    else if (collectionView == collectionViewTwo)
    {
        //create cell of type 2
    }
    return cell;
}

私のログでは、次の出力が得られます (例):

numberOfItemsInSectionのrestoreIdentifier: (null) sizeForItemAtIndexPathのrestoreIdentifier: (null) restoreIdentifier cellForItemAtIndexPath: collectionViewOne

collectionViewOne は、collectionViewOne の restoreIdentifier です。では、なぜ最初の 2 つの方法で認識されないのでしょうか。

もちろん、異なる CollectionView 内のセルのサイズとセルの数が適切に設定されていないため、結果としてクラッシュが発生します。エラー:

*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2903.2/UICollectionViewData.m:341
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x8c31c10> {length = 2, path = 0 - 2}'

どうすればこれを修正できますか?

4

2 に答える 2

10

これで解決です。問題は、両方の UICollectionView に同じ UICollectionViewFlowLayout を使用したことです。これにより、例外が発生しました。

最終的に、2 つのコントローラーに異なるクラスを使用しました。これにより、デリゲートのメソッドで適切な CollectionView を選択するという問題が解決されました。

ご意見をお寄せいただきありがとうございます。

于 2013-11-06T18:51:28.947 に答える
0

コレクション ビューのタグ プロパティを使用して、どのコレクション ビューがどれであるかを識別できます。このような:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier);

    if (collectionView.tag == collectionViewOneTag)
    {
        //create cell of type 1
    }
    else if (collectionView.tag == collectionViewTwoTag)
    {
        //create cell of type 2
    }

    return cell;
}

collectionViewOneTag と collectionViewTwoTag は、コードまたは xib ファイルで定義できる整数です。

それが役立つことを願っています。

于 2013-10-29T17:37:31.520 に答える