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;
}
参考サイト
関連記事
-
-
UIButtonのappearanceで派生する色設定
以下のコードでUIButtonのappearanceでBackgroundColorの設定をすると、
-
-
viewDidLoadとviewDidLayoutSubviewsのタイミング
真実の記事にある「コードでiPhone6を識別する」話とほんのちょっとだけ繋がっている話で、view
-
-
Xcode5.1のInterfaceBuilderにおける地味な変更点
iOS7.1に合わせてXcode5.1がリリースされました Xcode Release N
-
-
UIButtonのタイトル変更時のチラつき抑制
久しぶりにXcode+Objective-Cでアプリを作っていたら、UIButtonのタイトルを動的
-
-
iOSシミュレータの帯域制限をテストする
iOSシミュレータで通信帯域制限をテストしたいと思ったんですが、Xcode8からXcode7.xとは
-
-
iOS8のシミュレーターでLocalizationのテストをする
iOS8.1のシミュレーターでは、従前できていたシミュレーター内での「設定」(Setting)からの
-
-
UISplitViewControllerの仕切り線の色を設定
UISplitViewControllerで、iPadを横向き(Landscape)にしたときの2つ
-
-
UITableViewのセクションヘッダをStoryboardで作る時の参考
UITableViewのセクションヘッダをカスタムして作る際に、セルと同様にプロトタイピングするため
-
-
UITableViewのセルセパレータが画像の下部分だけ切れる場合の対応
iOS7のUItableViewCellだと、UITableView.imageViewに画
