TableViewCellsを使って画面推移ができるコードを記述しています。ページ推移は可能ですが、画像を加えるためのコードを記述するとエラーが出てしまいます。
画面推移置で参考にした動画
https://www.youtube.com/watch?v=BCSGh-YJgvs&t=174s
画像配置で参考にしたサイト
http://joyplot.com/documents/2016/11/11/swift-uitableview/
import
1import UIKit 2 3class TableViewController: UITableViewController { 4 5 var names = [String]() 6 var identities = [String]() 7 var imgArray = [String]() 8 9 override func viewDidLoad() { 10 11 names = ["セル1","セル2"] 12 identities = ["A","B"] 13 imgArray = ["img1","img2"] 14 } 15 16 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 17 return names.count 18 } 19 20 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 21 // tableCell の ID で UITableViewCell のインスタンスを生成 22 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 23 cell?.textLabel!.text = names[indexPath.row] 24 25 //画像を加えるためのコード 26 27 let img = UIImage(named:"\(imgArray[indexPath.row])") 28 29 let imageView = tableView.viewWithTag(1) as! UIImageView 30 imageView.image = img 31 32 33 return cell! 34 } 35 36 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 37 let vcName = identities[indexPath.row] 38 let ViewController = storyboard?.instantiateViewController(withIdentifier: vcName) 39 self.navigationController?.pushViewController(ViewController!, animated: true) 40 } 41} 42
エラー
fatal error: unexpectedly found nil while unwrapping an Optional value
2017-01-29 12:25:58.293533 TableViewjerd[1036:275067] fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/29 05:16 編集