33

以下のコードは、ヘッダー ビューを正しく表示しますが、UICollectionView の各セクションに対して:

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView * headerView =
        [collectionView 
            dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                               withReuseIdentifier:@"SectionHeaderCollectionReusableView"
                                      forIndexPath:indexPath];
    switch (indexPath.section) {
        case Section_One:
            return headerView;
        case Section_Two:
            return headerView;
        case Section_Three:
            return headerView;
        case Section_Four:
            return headerView;
        case Section_Five:
            return headerView;

        default:
            return headerView;
    }
}

代わりにやりたいことは、「Section_One」または「Section_Two」のヘッダー ビューを表示しないことですが、「nil」を返すと「NSInternalInconsistencyException」が発生します。

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView * headerView =
        [collectionView 
            dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                               withReuseIdentifier:@"SectionHeaderCollectionReusableView"
                                      forIndexPath:indexPath];
    switch (indexPath.section) {
        case Section_One:
            return nil;
        case Section_Two:
            return nil;
        case Section_Three:
            return headerView;
        case Section_Four:
            return headerView;
        case Section_Five:
            return headerView;

        default:
            return nil;
    }
}

特定のセクションのみのヘッダー ビューを表示するには、どうすればよいですか?

4

5 に答える 5

57

各セクションのヘッダーを返し、この UICollectionViewDelegate 関数でセクション ヘッダーのサイズをゼロに設定します。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeZero;
    }else {
        return CGSizeMake(self.collectionView.bounds.size.width, desiredHeight);
    }
}
于 2014-05-28T15:06:42.800 に答える
8

UICollectionViewController2 つの を制御する 1 つのケースがありUICollectionView(後でコレクション ビュー 1 および 2 として参照)、最初のビューにはヘッダーが必要で、2 番目のビューにはヘッダー (またはフッター) が必要ありませんでした。

@mwrightの答えに欠けているのは、次のようCGSizeZeroコレクションビュー2に戻ったときです。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    if collectionView == self.collectionView2 {
        return CGSizeZero
    }
    return < something else >
}

... は、コレクション ビュー 2に対してまったく呼び出されcollectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView ないことを意味します。

つまり、2 番目のコレクション ビューの "間違った" ヘッダーが無駄に返されることを心配する必要はありません。

于 2016-02-19T09:23:06.323 に答える
5

再利用可能なヘッダーに XIB で AutoLayout を使用すると、受け入れられた回答が壊れていることに気付きました。

コンテンツを端から離したり、ヘッダー ビュー内の項目に静的で不変のサイズを与えたりすると、特に問題が発生しました。ヘッダー サイズを CGSizeZero に設定すると、デリゲート メソッドで設定された要件を満たすためにすべての制約を破るという、Interface Builder からの数十の警告でデバッガー コンソールが乱雑になりました。

それ自体は技術的には災害ではありませんが、それでも汚れています。そして、Swift と AutoLayout の時代には、よりクリーンなソリューションが必要です。また、仕事中にそのようなものをクライアントに出荷したくはありません。

これを修正するために、呼び出しreferenceSizeForHeaderInSection:て返すだけでなく、CGSizeZero別のサブクラスUICollectionReusableViewwith XIB を作成し、その中のビューの高さを に設定しました0

その後、メソッドswitch内に含まれるステートメントの外でそのバリアントをデキューします。viewForSupplementaryElementOfKindこれは、Interface Builder視覚的な要件の両方を満たします。

とにかく、デバッグ中に何百もの満足できない制約の警告がコンソールに出力されます。

于 2016-06-17T02:05:41.400 に答える