TableViewのcellForRowAtにおいてcell.detailTextLabel!.textに日付を表示したいのですが、シミュレーターを起動するとThread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional valueというエラーがcell.detailTextLabel!.text = sDateの横に発生してしまいます。解決策を教えていただきたいです。
swift
1import UIKit 2 3class SaveViewController: UIViewController,UITableViewDelegate, 4UITableViewDataSource { 5 6 @IBOutlet var tableView: UITableView! 7 8 var resultArray = [String]() 9 //現在の日付を取得 10 let date:Date = Date()...... 11 12 13 14func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 15 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 16 //日付のフォーマットを指定する 17 let format = DateFormatter() 18 format.dateFormat = "yyyy/MM/dd HH:mm" 19 //日付をStringに変換する 20 let sDate = format.string(from: date) 21 22 cell.textLabel!.text = resultArray[indexPath.row] 23 cell.detailTextLabel!.text = sDate**Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value** 24 25 return cell 26 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。