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

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

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

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

Q&A

解決済

1回答

541閲覧

CollectionViewでcollectionViewLayoutを設定するとSectionHeaderのメソッドが呼ばれない

hodoru3sei

総合スコア284

Swift

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

0グッド

0クリップ

投稿2021/11/04 08:30

編集2021/11/04 08:33

概要

CollectionViewでSectionHeaderを表示させたいと考えているのですがうまくいかず困っています。
セルのサイズと左右のマージンなどを独自の値にしたいと考えているのでCollectionFlowLayoutをCollectionViewに設定しているのですがこれがも問題なのでしょうか?

どうやってどちらも設定すれば良いのでしょうか?

swift

1class ViewController: UIViewController { 2 3 @IBOutlet weak var collectionView: UICollectionView! { 4 didSet { 5 collectionView.delegate = self 6 collectionView.dataSource = self 7 collectionView.register(UINib(nibName: "SampleCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "Cell") 8 collectionView.register(UINib(nibName: "SampleSectionHeaderView", bundle: nil), 9 forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, 10 withReuseIdentifier: "SampleSectionHeaderView") 11 } 12 } 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 let layout = CollectionFlowLayout() 16 let sideMargin: CGFloat = 30 17 let sideAllMargin = sideMargin * 2 + 24 18 let width = (self.view.frame.width - sideAllMargin) / 2 19 layout.itemSize = CGSize(width: width, height: 300) 20 21 //セル同士の隙間 22 layout.minimumInteritemSpacing = 24 23 //セル同士の高さの隙間 24 layout.minimumLineSpacing = 24 25 26 layout.sectionInset = UIEdgeInsets(top: 16, left: sideMargin, bottom: 0, right: sideMargin) 27 collectionView.collectionViewLayout = layout 28 } 29 30 31} 32 33 34extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource { 35 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 36 return 20 37 } 38 39 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 40 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! SampleCollectionViewCell 41 cell.hogeView.backgroundColor = .white 42 return cell 43 } 44 45 // Sectionの設定 46 // 呼ばれる 47 func numberOfSections(in collectionView: UICollectionView) -> Int { 48 return 1 49 } 50 // 呼ばれない 51 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 52 return CGSize(width: self.view.frame.width, height: 100) 53 } 54 55   // 呼ばれない 56 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 57 let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, 58 withReuseIdentifier: "SampleSectionHeaderView", 59 for: indexPath) 60 return view 61 } 62 63 64} 65

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

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

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

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

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

guest

回答1

0

ベストアンサー

swift

1func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

上記のdelegateメソッドの第一引数は、 _ をつけて外部引数名省略で宣言する必要があります。

swift

1func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

それと、これは UICollectionViewDelegateFlowLayoutプロトコルのdelegateメソッドなので、UICollectionViewDelegateFlowLayout に準拠することを指定する必要があります。

その2つを修正すれば呼び出されるようになると思います。

投稿2021/11/05 01:17

TakeOne

総合スコア6299

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問