ViewController,SubViewController,CustomCell1,CustomCell2,View1,View2があり、
ViewControllerにViewとSegmentedControlを配置しており、
SegmentedControlをタップすることで、ViewにView1のTabelViewまたは、View2のTableViewが表示されるようになっております。
tableviewにいくつかのセルが表示されており、それぞれのセルには画像(imageView)、名前(itemName)、出身(from)が配置されております。
セルをタップすることで、SubViewControllerに遷移して、NavigationbarのTitle、ImageView、Labelに上記の値がそれぞれ表示されるといった実装を行いたいと考えております。
下記のコードで実行すると、セルをタップしたらprint(selectedName,”1”)でエラーが返ってきます。
ViewControllerへの値の受け渡しができていないのが原因かと思いますが、何か良い実装方法はありませんでしょうか。
回答の程、よろしくお願いいたします。
:出力エラー
fatal error: unexpectedly found nil while unwrapping an Optional value
:参考元
https://iphone-app-tec.com/ios/tableview-cell.html
http://qiita.com/BigSea/items/9aa35b95e5d4d1dc8a52
https://github.com/KentarouKanno/SegmentChangeTableViewController
ViewController
var selectedImage: UIImage? var selectedName : String! var selectedFrom : String! func selectCell(identifier: String, item: Friend) { performSegue(withIdentifier: identifier, sender: nil) } override func prepare(for segue: UIStoryboardSegue, sender: Any!) { if (segue.identifier == "toSubViewController") { let navigationTitle = segue.destination as? SubViewController navigationTitle?.selectedNme = self.selectedName print(navigationTitle,"Navi") print(selectedName,"1") navigationTitle?.selectedNme = selectedName let subVC: SubViewController = (segue.destination as? SubViewController)! print(selectedName,"2") subVC.selectedImg = selectedImage subVC.selectedFrm = selectedFrom } }
View1
// セクション数 func numberOfSectionsInTableView(TableView: UITableView) -> Int { return 1 } // セクションの行数 func tableView(_ tableView1: UITableView, numberOfRowsInSection section: Int) -> Int { return friends.count } // Generate Cell func tableView(_ tableView1: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: CustomCell1 = tableView1.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell1 cell.setCell(friend: friends[indexPath.row]) return cell } var selectedImage: UIImage? var selectedName : String! var selectedFrom : String! // Select Cell func tableView(_ tableView1: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView1.cellForRow(at: indexPath) as! CustomCell1 selectedImage = cell.imageView1.image selectedName = cell.itemName1.text! tableView1.deselectRow(at: indexPath, animated: true) selectedFrom = cell.from1.text! delegate?.selectCell(identifier: "toSubViewController", item: friends[indexPath.row]) }
View2
// セクション数 func numberOfSectionsInTableView(tableView2: UITableView) -> Int { return 1 } // セクションの行数 func tableView(_ tableView2: UITableView, numberOfRowsInSection section: Int) -> Int { return friends.count } // Generate Cell func tableView(_ tableView2: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: CustomCell2 = TableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell2 cell.setCell(friend: friends[indexPath.row]) return cell } var selectedImage: UIImage? var selectedName : String! var selectedFrom : String! // Select Cell func tableView(_ tableView2: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView2.cellForRow(at: indexPath) as! CustomCell2 selectedImage = cell.imageView2.image selectedName = cell.itemName2.text! TableView.deselectRow(at: indexPath, animated: true) selectedFrom = cell.from2.text! delegate?.selectCell(identifier: "toSubViewController", item: friends[indexPath.row]) }
CustomCell1(CustomCell2も似たような実装)
override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } func setCell(friend :Friend) { self.itemName1.text = friend.itemName as String self.from1.text = friend.from as String do { let imageData :NSData = try NSData(contentsOf: friend.imageUrl! as URL,options: NSData.ReadingOptions.mappedIfSafe) self.imageView1.image = UIImage(data:imageData as Data) } catch { print("Error: can't create image.") } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。