46

UICollectionViewを使用して複数のアルバムを表示するプロジェクトに取り組んでいます。アイテムは問題なく表示されますが、最初のセクションの上にヘッダーを表示したいと思います。

これを行うためregisterNib:forSupplementaryViewOfKind:withReuseIdentifier:に、 init メソッドに を追加しました。このような:

[self.collectionView registerNib:[UINib nibWithNibName:@"AlbumHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kAlbumHeaderIdentifier];

( AlbumHeaderNib にAlbumHeaderは、 のサブクラスであるクラス のビューが含まれていますUICollectionView。)

その後、collectionView:viewForSupplementaryElementOfKind:atIndexPathメソッドを実装しました:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    return [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kAlbumHeaderIdentifier forIndexPath:indexPath];
}

これで、ヘッダー ビューを読み込もうとするはずです。しかし、そうではありません。補足ビューのメソッドは呼び出されません。

私は何が欠けていますか?何時間も立ち往生し、s のドキュメントをUICollectionView何度も読みましたが、何も役に立たないようです。何かご意見は?

4

3 に答える 3

123

yuf が尋ねた方法を探した後、デフォルトでヘッダー/フッターのサイズが 0,0 であることを読みました。サイズが 0 の場合、ヘッダー/フッターは表示されません。

プロパティでサイズを設定できます。

flowLayout.headerReferenceSize = CGSizeMake(0, 100);

次に、すべてのヘッダーが同じサイズになります。セクションごとに異なる必要がある場合は、UICollectionViewDelegateFlowLayoutプロトコルの一部である次のメソッドを実装できます。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == albumSection) {
        return CGSizeMake(0, 100);
    }

    return CGSizeZero;
}

height垂直スクロールではコレクション ビューの戻り値と全幅が使用され、水平スクロールではコレクション ビューの戻り値widthと全高が使用されることに注意してください。

于 2012-10-03T09:49:10.680 に答える
3

実装しましたか:

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

1つのことを機能させるためだけに実装するメソッドはたくさんあります...私も学んでいます。それが機能するかどうか教えてください。

編集:申し訳ありませんが間違った方法。それはサブクラス化のためだと思います。私が話しているのはUICollectionViewLayout(レイアウトが補足ビューをサポートしている場合は、サブクラス化するレイアウトオブジェクト)です。

- layoutAttributesForSupplementaryViewOfKind:atIndexPath:

こちらをご覧ください:https ://developer.apple.com/library/ios/#documentation/UIKit/Reference/UICollectionViewLayout_class/Reference/Reference.html#//apple_ref/occ/cl/UICollectionViewLayout

于 2012-10-03T01:09:03.723 に答える