前提・実現したいこと
ここに質問の内容を詳しく書いてください。
ボタンで遷移した先のViewController上にあるCollectionViewにて
Assetsフォルダ内の画像(UIImage)を一覧表示したいのですが、
ビルドはできるのですが、ボタンを押すと落ちてしまいます。
発生している問題・エラーメッセージ
Could not cast value of type 'UIView' (0x7fff898df878) to 'UIImageView' (0x7fff898df490).
該当のソースコード
swift
1 2 import UIKit 3 4 class FaceItemViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate{ 5 6 private let faces = ["face0", "face1", "face2", "face3", "face4", "face5","face6", "face7", "face8", "face9", "face10", "face11","face12", "face13", "face14"] 7 8 @IBOutlet weak var collectionView: UICollectionView! 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 // レイアウトを調整 14 let layout = UICollectionViewFlowLayout() 15 layout.sectionInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15) 16 collectionView.collectionViewLayout = layout 17 18 19 } 20 21 22 //セルの表示数 23 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 24 return faces.count; 25 } 26 27 // セル(要素)に表示する内容 28 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 29 30 let cell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier:"FaceCell",for: indexPath ) 31 32 //Tag番号を使ってインスタンスをつくる 33 let imageView = cell.contentView.viewWithTag(1) as! UIImageView 34 //画像配列の番号で指定された要素の名前の画像をUIimageとする 35 let cellImage = UIImage(named: faces[indexPath.row]) 36 //UIimageをUIimageViewのimageとして設定 37 imageView.image = cellImage 38 39 cell.backgroundColor = .white 40 41 return cell 42 43 } 44 45 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 46 let horizontalSpace : CGFloat = 20 47 let cellSize : CGFloat = self.view.bounds.width / 3 - horizontalSpace 48 return CGSize(width: cellSize, height: cellSize) 49 } 50 51 52 } 53 } 54 55 56
試したこと
インスタンスは作らず、一枚の画像を連続(複数)表示することはできました。
ストーリーボード上で、遷移先のviewのcustumclassをUIViewに設定して
みましたがダメでした。
ご教示いただければ幸いです。どうぞよろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
Xcode11.3
回答1件
あなたの回答
tips
プレビュー