質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2881閲覧

Swift:TableViewの値を別ビューに受け渡す

YonamineTK

総合スコア42

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/01/10 10:11

編集2017/01/10 10:20

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.") } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決いたしました。

if (changeSegment.selectedSegmentIndex == 0) {
navigationTitle?.selectedNme = self.view1.selectedName
selectedImage = self.view1.selectedImage
selectedFrom = self.view1.selectedFrom
} else {
navigationTitle?.selectedNme = self.view2.selectedName
selectedImage = self.view2.selectedImage
selectedFrom = self.view2.selectedFrom
}
let subVC: SubViewController = (segue.destination as? SubViewController)!
subVC.selectedImg = selectedImage
subVC.selectedFrm = selectedFrom
}

投稿2017/01/12 04:44

YonamineTK

総合スコア42

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問