質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Q&A

1回答

4287閲覧

UICollectionViewのheaderの表示内容をsectionによって変更したい

syoshinsya

総合スコア35

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

1グッド

1クリップ

投稿2016/08/12 01:25

#import "ViewController.h" @interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate> { NSArray *images; NSArray *titles; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. titles = @[ @"海", @"山", ]; images = @[ @[@"photo1.jpg", @"photo2.jpg", @"photo1.jpg"], @[@"photo1.jpg", @"photo1.jpg", @"photo1.jpg"], ]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (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.backgroundColor = [UIColor greenColor]; reusableview = headerView; } return reusableview; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return titles.count; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return [images[section] count]; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell; cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; UIImageView *imageView = (UIImageView *)[cell viewWithTag:1]; imageView.image = [UIImage imageNamed:images[indexPath.section][indexPath.row]]; return cell; } @end

実現したいこと

・ヘッダーにタイトルを設定したい(titlesの中身をセクション毎に表示したい)
・セクション毎にヘッダーの色を変更したい

試したこと

・Collection Reusable ViewにstoryboardからLabelを配置
ビルドするとLabelとヘッダーに表示される
・上記で設置したLabelをコードに紐付けようとするとエラーがでる
Outlet cannot be connected to repeating content
・ヘッダータイトルを表示をさせようと下記を記述

NSString *title = [[NSString alloc]initWithFormat:@"%@",titles]; headerView.title.text = title;

エラーが表示される:property not found on object of type uicollectionview

基本的な質問で申し訳ありませんが
ネット等で解決策を探したのですが、中々解決できなかったので
質問させていただきます。
どなたか、ご教示いただけましたら幸いです。
よろしくお願いいたします。

keisei_1092👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

以下のようにすることでセクションヘッダーのタイトル、背景色を変更できました。
試してみてください。

objective

1@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate> { 2 NSArray *images; 3 NSArray *titles; 4 NSArray *colors; 5} 6@end 7 8@implementation ViewController 9 10- (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 titles = @[ 14 @"海", 15 @"山", 16 ]; 17 colors = @[ 18 [UIColor blueColor], 19 [UIColor greenColor], 20 ]; 21 images = @[ 22 @[@"photo1.jpg", @"photo2.jpg", @"photo1.jpg"], 23 @[@"photo1.jpg", @"photo1.jpg", @"photo1.jpg"], 24 ]; 25} 26 27- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView 28 viewForSupplementaryElementOfKind:(NSString *)kind 29 atIndexPath:(NSIndexPath *)indexPath { 30 UICollectionReusableView* reusableview = nil; 31 32 if (kind == UICollectionElementKindSectionHeader) { 33 // --- ヘッダ 34 UICollectionReusableView* headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 35 withReuseIdentifier:@"HeaderView" 36 forIndexPath:indexPath]; 37 UILabel *label = [headerView viewWithTag:1]; 38 label.text = titles[indexPath.section]; 39 headerView.backgroundColor = colors[indexPath.section]; 40 41 reusableview = headerView; 42 } 43 return reusableview; 44}

a
w
a

投稿2016/08/12 10:57

編集2016/08/12 10:58
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問