前提・実現したいこと
iOSアプリを初めて作っているのですがエラーが発生して作業が止まってしまっています。。
発生している問題・エラーメッセージ
Thread 1: Fatal error: Unexpectedly found nil while unwrappin
エラーが出ているのは下記ソースの「ここでエラーが発生」の部分です
該当のソースコード
swift
1import UIKit 2 3private let reuseIdentifier = "Cell" 4 5class BooksCollectionViewController: UICollectionViewController { 6 7 let movieData: [[String]] = [["タイトル1", "movie1.mp4","image1"],["タイトル2", "movie1.mp4","image2"]] 8 9 override func viewDidLoad() { 10 11 // background image 12 let bg = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)) 13 bg.image = UIImage(named: "sky") 14 bg.layer.zPosition = -1 15 self.view.addSubview(bg) 16 17 super.viewDidLoad() 18 19 // Uncomment the following line to preserve selection between presentations 20 // self.clearsSelectionOnViewWillAppear = false 21 22 // Register cell classes 23 self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 24 25 // Do any additional setup after loading the view. 26 } 27 28 /* 29 // MARK: - Navigation 30 31 // In a storyboard-based application, you will often want to do a little preparation before navigation 32 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 33 // Get the new view controller using [segue destinationViewController]. 34 // Pass the selected object to the new view controller. 35 } 36 */ 37 38 // MARK: UICollectionViewDataSource 39 40 override func numberOfSections(in collectionView: UICollectionView) -> Int { 41 // #warning Incomplete implementation, return the number of sections 42 return 1 43 } 44 45 46 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 47 // #warning Incomplete implementation, return the number of items 48 return self.movieData.count 49 } 50 51 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 52 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) 53 54 // Configure the cell 55 let imageView = cell.contentView.viewWithTag(1) as! UIImageView //「ここでエラーが発生」 56 57 // 画像配列の番号で指定された要素の名前の画像をUIImageとする 58 let cellImage = UIImage(named: self.movieData[indexPath.row][2]) 59 60 // UIImageをUIImageViewのimageとして設定 61 imageView.image = cellImage 62 63 // Configure the cell 64 65 return cell 66 } 67 68} 69
試したこと
UIImageにはTagに1を設定しています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/24 15:07
2019/05/24 15:15