【Swift】CollectionViewにCustomCellを連携させる[備忘録]
CollectionViewにUICollectionViewCellのCustomCellを適用する方法

override func viewDidLoad() {
super.viewDidLoad()
self.setSearchController()
self.collectionView.dataSource = self
self.collectionView.delegate = self
collectionView.register(UINib(nibName: "CategoryCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CategoryCollectionViewCellID")
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 18
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: CategoryCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCollectionViewCellID", for: indexPath) as! CategoryCollectionViewCell
cell.setCell()
return cell
}
}
また、Cellのサイズを動的に変更する場合。
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.view.frame.size.width / 3.1, height: self.view.frame.size.width / 3.1)
}
}

詳細! Swift 4 iPhoneアプリ開発 入門ノート Swift 4 + Xcode 9対応
- 作者: 大重美幸
- 出版社/メーカー: ソーテック社
- 発売日: 2017/11/03
- メディア: 単行本
- この商品を含むブログを見る
![[改訂新版]Swiftポケットリファレンス (POCKET REFERENCE) [改訂新版]Swiftポケットリファレンス (POCKET REFERENCE)](https://images-fe.ssl-images-amazon.com/images/I/51L6-N7qRyL._SL160_.jpg)
[改訂新版]Swiftポケットリファレンス (POCKET REFERENCE)
- 作者: WINGSプロジェクト片渕彼富,山田祥寛
- 出版社/メーカー: 技術評論社
- 発売日: 2018/03/09
- メディア: 単行本(ソフトカバー)
- この商品を含むブログを見る