コレクションビューのヘッダーを設定したいので、メソッドを実装しましたfunc collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView
。
ただし、switchステートメントが機能しないようです。セクションに応じて分岐してヘッダービュー内にラベルを設定しようとしても、すべてのセクションの結果のヘッダービューには、私が書いたすべてのラベルが含まれています。
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath)
let headerLabel = UILabel(frame: CGRectMake(2, 8, 120, 24))
headerView.addSubview(headerLabel)
print(indexPath.section)
switch (indexPath.section) {
case 0:
headerLabel.text = "A"
return headerView
case 1:
headerLabel.text = "B"
return headerView
default:
break
}
return headerView
default:
assert(false, "Unexpected element kind")
}
}
上記のコードでは、両方のセクションのラベルにAとBの両方のラベルがあり、互いに重なり合っています。
私の場合、スイッチが機能しないのはなぜですか?
私のコレクションビューのコンテンツはサーバーからデータをフェッチするため、print(indexPath.section)
が 2 回実行され、それぞれが0
と1
がこの順序で出力されます。
これは問題に関連していますか?