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

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

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

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

874閲覧

Swift コレクションビューのheaderにそれぞれのタイトルを表示したい

mitci

総合スコア37

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/12/17 12:59

編集2017/12/17 13:02

###前提・実現したいこと
コレクションビューのヘッダーに、テーブルビューのセクションタイトルのように、それぞれタイトルを表示させたくて、以下のobjective-Cのコードを自分なりにswift3に書き直しました。

Objective

1- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView 2 viewForSupplementaryElementOfKind:(NSString *)kind 3 atIndexPath:(NSIndexPath *)indexPath { 4 UICollectionReusableView* reusableview = nil; 5 6 if (kind == UICollectionElementKindSectionHeader) { 7 // --- ヘッダ 8 UICollectionReusableView* headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 9 withReuseIdentifier:@"HeaderView" 10 forIndexPath:indexPath]; 11 UILabel *label = [headerView viewWithTag:1]; 12 label.text = titles[indexPath.section]; 13 14 reusableview = headerView; 15 } 16 return reusableview; 17}

↓こうしました↓

swift

1 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 2 var reusableView = nil //エラー1 3 if kind == UICollectionElementKindSectionHeader { 4 let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderView", for: indexPath) 5 let label = headerView.viewWithTag(1) 6 label.text = self.sectionData[section].title //エラー2 7 reusableView = headerView 8 } 9 return headerView //エラー3 10 }

###発生している問題・エラーメッセージ

var reusableView = nil //エラー1 <'nil' requires a contextual type //省略 label.text = self.sectionData[section].title //エラー2 <Value of type 'UIView?' has no member 'text' //省略 return headerView //エラー3 <Use of unresolved identifier 'headerView'

とりあえず、let headerViewの部分がうまくはまっていないみたいなのはわかるのですが・・・
エラー1についてのヘッダーのリユースについての使い方もよくわかっていません・・・

ちなみに、StoryBoard上でCollectionReusableViewのidentifierを"HeaderView"にして、
その中に配置したラベルのタグは1にしてあります。

###試したこと
わたしにわかったのは、何かしらreusableに特別な指定方法がありそうということheaderViewが何者かをswiftが理解していないことそのせいでheaderViewのtag1がラベルだと気がついてもらえていないことくらいでした・・・

###補足情報(言語/FW/ツール等のバージョンなど)
xcode8 swift3

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

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

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

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

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

guest

回答1

0

ベストアンサー

(ほぼ)そのまま置き換えただけで動作確認などは行っていません。

collectionView(_:viewForSupplementaryElementOfKind:at:)はnilを返してはいけないので、元記事のコードは適切ではありません。下記コードでは、とりあえずUICollectionReusableView()を返すようにしています。

UILabelのエラーはキャスト、それ以外はオプショナル型のエラーです。
オプショナル型に関してはSwiftを使っていく上で避けて通れませんので、先に進む前に勉強しておくことをお勧めします。

swift

1func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 2 3 var reusableview = UICollectionReusableView() 4 5 if kind == UICollectionElementKindSectionHeader { 6 let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderView", for: indexPath) 7 if let label = headerView.viewWithTag(1) as? UILabel { 8 label.text = titles[indexPath.section] 9 } else { 10 print("is not UILabel") 11 } 12 reusableview = headerView 13 } 14 15 return reusableview 16}

投稿2017/12/18 00:47

編集2017/12/18 00:48
fuzzball

総合スコア16731

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

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

mitci

2017/12/18 11:51

これでエラーを吐かなくなりました!別の理由で実機検証はできていないですが・・・ オプショナル型とキャストは参考書を読んでいてもよくつまずいていた場所なので、重点的に勉強したいと思います・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問