ストーリーボード未使用で、コードのみで画像のビューを実装しました。
コードの成り立ちが自分で理解できているかを確認したいです。
いくつか気になる部分を見ていただきたいので、
誤りがある場合はご指導お願いいたします。(AppDelegateは省いています。)
・WordCellクラス内にsetupViews()をインスタンス化
WordCellの四方のアンカーがテキストによって変動可能な様にisActiveをtrueにする、アンカーに対応できるCGRectをframe:に指定。
・グローバルスコープで各Id(cell,header,footer)を定義して、
この3つを登録=registerしないと反映されないので、
registerでは、cellはWordCell、headerとfooterはUICollectionViewCellを指定。他と干渉しない様にselfをつける。
collectionViewの要素として定義するパターンで、UICollectionElementKindSectionHeader,hooterを選択、各Idも指定。
・if kind == では、header,footer両方一緒にdequeueできる形のif文を使用。
import UIKit //ワードセル class WordCell: UICollectionViewCell { //this gets called when a cell is dequeued override init(frame: CGRect) { super.init(frame: frame) setupViews() } let wordLabel: UILabel = { let label = UILabel() label.text = "TEST TEST TEST" label.translatesAutoresizingMaskIntoConstraints = false return label }() func setupViews() { backgroundColor = .yellow addSubview(wordLabel) wordLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true wordLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true wordLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true wordLabel.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } //コントローラー セルID class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { let cellId = "cellId" let headerId = "headerId" let footerId = "footerId" override func viewDidLoad() { super.viewDidLoad() collectionView?.backgroundColor = .white collectionView?.register(WordCell.self, forCellWithReuseIdentifier: cellId) collectionView?.register(UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId) collectionView?.register(UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: footerId) } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 4 } //セル override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) return cell } //セル func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width, height: 50) } //ヘッダー override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { //if ヘッダー フッター if kind == UICollectionElementKindSectionHeader { let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) header.backgroundColor = .blue return header } else { let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: footerId, for: indexPath) footer.backgroundColor = . green return footer } } //ヘッダー func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSize(width: view.frame.width, height: 50) } //フッター func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSize(width: view.frame.width, height: 100) } }
回答1件
あなたの回答
tips
プレビュー