1

ストーリーボードにUICollectionViewを作成し、ヘッダーフッタービューを追加して正常に動作しました.試した

- (void)setUpCustomCollectionView
{

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, 320, 500) collectionViewLayout:layout];

    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"brandingHeaderView"];

    self.collectionView.bounces = NO;
    self.collectionView.tag = 10;
    self.collectionView.backgroundColor = [UIColor darkGrayColor];
    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];

    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;

    [self.baseScrollView addSubview:self.collectionView];
}

そしてデリゲートで

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath
{
 if (kind == UICollectionElementKindSectionHeader) {
            UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"brandingHeaderView" forIndexPath:indexPath];

            UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 80)];
            view.backgroundColor = [UIColor redColor];

                 [headerView addSubview:view];

            return headerView;
        }
}

教えて。

4

5 に答える 5

1

次のデリゲートが呼び出されないという同様の問題が発生しています...

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

次に、UICollectionViewFlowLayout のインスタンスを定義していたときに、次のコードに従って itemSize 値を割り当てたことを思い出しました...

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(106.f, 106.f);

ヘッダーにも次の行を追加してみてください...

layout.headerReferenceSize = CGSizeMake(320.f, 30.f);
于 2014-09-20T00:45:31.043 に答える
0

ここで間違いだと思います:

[UICollectionViewFlowLayout class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader

UICollectionViewFlowLayoutヘッダー ビューにできません

編集:

それを機能させるには、のサブクラスが必要です。プロパティのUICollectionReusableViewオーバーライドを忘れないでください。reuseIdentifierドキュメントも確認してください:

UICollectionReusableView クラス リファレンス

于 2014-04-03T11:01:36.377 に答える
0

これを追加するには、Header(Header.xib) という名前のカスタム nib ファイルを作成し、UILabel をオブジェクト ライブラリからドラッグして Header.xib に追加する必要があります。次に、UICollectionReusableView のサブクラスであるカスタム ファイルが作成されます。たとえば、HeaderCollectionReusableView.swift と header.xib が表示され、ラベルの IBOutlet がこのカスタム クラス内で実行されます。

于 2015-12-21T09:12:53.213 に答える
-1

ヘッダー ビューをプログラムで UICollectionView に追加するには、次のことを行う必要があります。

UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout alloc] init];
layout.headerReferenceSize = CGSizeMake(100.0f, 40.0f);

UICollectionView* _collectionView=[[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:UICollectionElementKindSectionHeader];

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath

if ([kind isEqualToString:UICollectionElementKindSectionHeader]){

UICollectionReusableView *reusableView = [collectionView      dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:UICollectionElementKindSectionHeader forIndexPath:indexPath];

        if (reusableView==nil) {
        reusableView=  [[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        label.text= @"Top stories";
        label.textColor = [UIColor blueColor];
        [reusableView addSubview:label];
        }
        return reusableView;
    }
    return nil;
}
于 2016-05-16T09:04:10.233 に答える