以下のように指定するとThread 1: signal SIGABRT
と出てアプリが落ちてしまいます。
swift
1 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 2 performSegue(withIdentifier: "goSubDelivery", sender: nil) 3 } 4 5 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 6 if segue.identifier == "goSubDelivery" { 7 let subvc = segue.destination as! SubViewController 8 } 9 }
しかし、以下のようにすると解決されます。
swift
1 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 2 performSegue(withIdentifier: "goSubDelivery", sender: nil) 3 }
なぜこのようになるのでしょうか?最近iOS
を勉強しはじめたので、初心者でもわかるような解説をしていただきたいです。よろしくお願いします。
全体のコードは以下のようになっていて、GithubAPIからrepository
を取得してテーブルに表示しています。
swift
1import UIKit 2import Foundation 3 4class DeliveryController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 @IBOutlet weak var TableView: UITableView! 6 7 var repositories: [Repository]? 8 9 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 10 return repositories?.count ?? 0 11 } 12 13 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 14 let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "repositoryCell", for: indexPath) 15 cell.textLabel!.text = repositories![indexPath.row].name! 16 return cell 17 } 18 19 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 20 return 80 21 } 22 23 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 24 performSegue(withIdentifier: "goSubDelivery", sender: nil) 25 } 26 27 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 28 if segue.identifier == "goSubDelivery" { 29 let subvc = segue.destination as! SubViewController 30 } 31 } 32 33 override func viewDidLoad() { 34 super.viewDidLoad() 35 // Do any additional setup after loading the view, typically from a nib. 36 } 37 38 override func didReceiveMemoryWarning() { 39 super.didReceiveMemoryWarning() 40 // Dispose of any resources that can be recreated. 41 } 42} 43
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。