28
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【iOS】CollectionViewにヘッダーとフッター両方を追加する

Last updated at Posted at 2014-07-20

CollectionViewにヘッダーとフッダーを表示したいと思って、以下のようにstoryboardを組んだのはいいものの、
collectionView:viewForSupplementaryElementOfKind:atIndexPath: 内で、return headerView;とか、return footerView; だと1つしか返せないので、ヘッダーとフッダーの出し分け方法を探したのだけれど、少し時間がかかったのでメモ。

スクリーンショット 2014-07-20 21.39.49.png

kindでヘッダーかフッダーか判断して、該当のビューを返す

  • kind == UICollectionElementKindSectionHeader → ヘッダー
  • kind == UICollectionElementKindSectionFooter → フッダー
/* ヘッダービューを(補助ビュー)生成してビューに返す */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader) {
        headerView = [self.mCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];

        //headerに画像をセット
        NSURL* imageUrl = [NSURL URLWithString:brandDetail.dBrandIconImage];
        UIImage *placeholderImage = [UIImage imageNamed:@"code.png"];
        [headerView.mBrandView sd_setImageWithURL:imageUrl placeholderImage:placeholderImage];
        headerView.mBrandName.text = brandDetail.dBrandName;

        reusableview = headerView;
    }

    if (kind == UICollectionElementKindSectionFooter){
        footerView = [self.mCollectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];


        reusableview = footerView;
    }

    return reusableview;

}

何か間違ございましたら、ご指摘お願いします。

28
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?