LoginSignup
0
1

More than 3 years have passed since last update.

UICollectionViewのヘッダー(header)の数の関数でハマった(numberOfSections)[Swift 5]

Posted at

現状

CollectionViewのheaderの数を増やしたい時にはまったのでメモ
headerを2つに増やしたいだけなのに

これはダメ(breakpoint通らない)

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 2
}

これもダメ(breakpoint通らない)

func numberOfSections(collectionView: UICollectionView) -> Int {
        return 2
}

注意点

CollectionViewのheader(つまりsection)の数を増やすには
numberOfSections(in collectionView) ->ヘッダーの数
collectionView(_ collectionView, numberOfItemsInSection)->セクションごとのcellの数
collectionView(_ collectionView, cellForItemAt)->セクションごとのcellの内容
この3つを変更する必要がある。

overrideするわけではないので関数名や引数名が間違っているとDelegateではなく独自関数として扱われ(多分)
buildが通ってしまうので要注意
(Delegateは関数名, 引数名は要確認)

対策

この通りに書けばいけます

func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
}

参考

【Swift】UICollectionReusableViewの使い方。ヘッダー、フッターのセクションに使われる部品 | はじはじアプリ体験記
->めちゃくちゃ参考になったんですが、情報が古いのかheaderの数のところだけ間違っていました

UICollectionViewのHeaderカスタマイズいろいろ - Qiita
【Swift】UICollectionViewをコードで実装する - Swift・iOS

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1