問題点
こちらのサイトを参考に、TableViewを設置し、cellに2つのUILabelと1つのUIImageViewを設置しました。これらにはそれぞれ1~3のtagを設定しています
しかし、追加したUILabelに対し、
swift
1Fatal error: Unexpectedly found nil while unwrapping an Optional value
とのエラーが出てしまいます。
UILabelを外した場合、UIImageViewは問題なく表示されるため、UILabelの方に問題があると思われます。
原因について、お教えいただけたらと思います。
コード
swift
1// 2// AlbumListViewController.swift 3// ARalbum 4// 5// Created by ryotaro tsuji on 2022/07/09. 6// 7 8import UIKit 9import RealmSwift 10 11class AlbumListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{ 12 13 @IBOutlet weak var tableView: UITableView! 14 15 var AlbumArray = try! Realm().objects(Album.self).sorted(byKeyPath: "date", ascending: true) 16 17 override func viewDidLoad() { 18 super.viewDidLoad() 19 20 tableView.delegate = self 21 tableView.dataSource = self 22 23 24 25// // カスタムセルを登録する 26// let Addnib = UINib(nibName: "AddTableViewCell", bundle: nil) 27// tableView.register(Addnib, forCellReuseIdentifier: "AddCell") 28 } 29 30 31 32 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 33 return AlbumArray.count + 1 34 } 35 36 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 37 38 if indexPath.row == 0 { 39 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 40 cell.backgroundColor = .gray 41 return cell 42 }else{ 43 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) 44 let cellImageView = cell.contentView.viewWithTag(3) as! UIImageView 45 let titleLabel = cell.contentView.viewWithTag(1) as! UILabel 46 let dateLabel = cell.contentView.viewWithTag(2) as! UILabel 47 48 let album = AlbumArray[indexPath.row - 1] 49 titleLabel.text = album.title 50 dateLabel.text = "\(album.date)" 51 52 let showAlbumImageList = try! Realm().objects(AlbumImage.self).filter("parentID == %@", album.id) 53 if showAlbumImageList.count != 0{ 54 let showAlbumImage = showAlbumImageList.first 55 let image_url = showAlbumImage?.directory 56 57 let path = getFileURL(fileName: image_url!).path 58 59 if FileManager.default.fileExists(atPath: path) { 60 if let imageData = UIImage(contentsOfFile: path) { 61 cellImageView.image = imageData 62 print(imageData) 63 } 64 else { 65 print("Failed to load the image.") 66 } 67 } 68 else { 69 print("Image file not found.") 70 } 71 72 } 73 74 75 76 return cell 77 } 78 79 } 80 81 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 82 83 // セルの選択を解除 84 tableView.deselectRow(at: indexPath, animated: true) 85 86 if indexPath.row == 0{ 87 let choiceModuleViewController = self.storyboard?.instantiateViewController(withIdentifier: "ChoiceModule") as! ChoiceModuleViewController 88 self.present(choiceModuleViewController, animated: true, completion: nil) 89 } 90 91 } 92 93 func getFileURL(fileName: String) -> URL { 94 let docDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! 95 return docDir.appendingPathComponent(fileName) 96 } 97 98 99} 100
> これらにはそれぞれ1~3のtagを設定しています
Tagが違ってviewWithTag()の戻りがnilになっているのでしょうか。
Xcodeの右側のAttributesインスペクタのViewセクションのTagが含まれる形で
キャプチャを貼り付けていただけますでしょうか。
(質問欄は編集できます。画像の挿入で画像が貼り付けられます)
ご回答ありがとうございます!
ご質問いただいて、改めてみたところ、tagではなく、Linesの数字を変更していました、、、
原因としては、tagが0のままだったことです。
ドツボにハマっており気がついていませんでした。
コメントありがとうございます。
解決したようで良かったです。
ご自身で回答を入力して自己解決の形で終了すると良いと思います。
回答1件
あなたの回答
tips
プレビュー