UICollectionViewのヘッダとフッタの設定
公開日:
:
Tips InterfaceBuilder, UICollectionView, Xcode
UICollectionViewで慣れないのがヘッダフッタで、毎度「どうやって設定するんだっけ…」と悩むので、備忘録を兼ねて記事を書いておこうと思います。
UITableViewと違う
NumberOf〜系統のメソッドが大体同じ感じなのでheader〜とかfooter〜とかいうメソッドがあるのかと思うと不意打ち食らうというか、これらはSupplementaryElement(サプリメント:補助的)として扱われています。
なので、[collectionView:viewForSupplementaryElementOfKind:atIndexPath:]というメソッドで対応するビューを引っ張ります。
クラスはUICollectionReuseableViewというクラスで、IBで作る場合は専用のタイトルセル(UICollectionViewCellの隣にある、Collectionの横長の棒がハイライトされているアイコン)をIBに入れて作ることができます。
IB使って実際にやるなら、派生クラスを作ってIBOutletをゴリゴリ割り当てておくと、メソッド内のデータの設定が楽になるかと思います。
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { // セクションヘッダ・フッタを引っ張ってくる UICollectionReusableView* reusableview = nil; if (kind == UICollectionElementKindSectionHeader) { // --- ヘッダ UICollectionReusableView* headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; //(ここにheaderViewの中身の定義をindexPath.sectionなんかをベースに設定してやる) reusableview = headerView; } else if (kind == UICollectionElementKindSectionFooter) { // --- フッタ UICollectionReusableView* footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FooterView" forIndexPath:indexPath]; //(ここにfooterViewの中身の定義をindexPath.sectionなんかをベースに設定してやる) reusableview = footerView; } return reusableview; }
参考サイト
関連記事
-
iPadのUIModalPresentationFormSheetでキーボードが閉じない
iPadでUIModalPresentationFormSheet(全画面ではなく、中央にフロートし
-
iPad用storyboard⇔iPhone用Storyboardへの変換
iPad用アプリとしてiPad用前提のstoryboardから、iPhone対応にするにあたり、si
-
UILabelの改行をInterfaceBuilderから入力する
UILabelの行数は任意に設定できて、「0」を指定すると行数を限定しないで(UILabelのbou
-
UIActionSheetが消えるときにキーボード表示/非表示通知が来る
UITextViewやUITextField編集中にアクションシートを表示すると、アクションシート
-
CoreDataのソートとsectionNameKeyPathの関係
CoreDataをフェッチするときに、indexPathのsectionにできるsectionNam
-
UICollectionViewが画面回転した時にレイアウトが崩れた時の対応
UICollectionViewCellのサイズを、内容に合わせて動的に変えた時に起こる問題に関して
-
User Defined Runtime Attributeについての覚書
UILabelに上下左右のパディングをつける方法を探して、結局「自分で以下のようなメソッドを持った拡
-
コードでiPhone6 Plusを識別する簡単な方法
表題の通り、コードでiPhone6 Plusを識別する簡単な方法です。 iPhone6 Plu