[swift] collectionViewController (xcode9 : swift4) テンプレート
collectionViewController (xcode9 : swift4) テンプレートを下記に記しておく
import UIKit class TestCollectionViewController: UICollectionViewController { override func viewDidLoad() { super.viewDidLoad() } //データの個数を返すメソッド override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 7 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCell", for: indexPath as IndexPath) let imageView = cell.contentView.viewWithTag(1) as! UIImageView imageView.image = UIImage(named: "image" + String(indexPath.row) + ".png") return cell } override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { //セグエを実行する。 performSegue(withIdentifier: "TestSegue", sender: nil) print("selected") } //画面遷移実行前の呼び出しメソッド override func prepare(for segue: UIStoryboardSegue, sender: Any?) { //選択中のセルの画像を取得する。 let index = collectionView?.indexPathsForSelectedItems let cell = collectionView?.cellForItem(at: index![0]) let imageView = cell!.viewWithTag(1) as! UIImageView //その他の処理 } }