UITableViewController+UINavigationControllerによる
画面遷移の方法について質問させてください。
まずはじめに、UITableViewController+UINavigationControllerの画面遷移の方法は
下記の様なdidSelectRowAtIndexPathプロパティの中でpushViewControllerを
定義して画面遷移させる方法が一般的だと思います。
Swift
1 func tableView(tableView: UITableView, didSelectRowAtIndexPath index 2Path: NSIndexPath) { 3 4 switch indexPath.row{ 5 case 1: 6 self.navigationController?.pushViewController(HogeViewController(), animated: true) 7 default: 8 break 9 10 } 11 }
上記の方法は、既に各indexPath.rowに格納されるセルが固定の場合は
使えますが、それぞれの列に格納される種類が変動する場合には
使用できません。
(例えば、アプリの設定画面などで使用されているUITableViewControllerの場合は
表示の順序が固定のため上記方法が適用できますが、SNSの通知画面など
表示されるセルが動的に変化する場合は使用できないと思います)
その上で、今回質問させていただきたいのは、表示されるセルの順序が動的に変化する場合においてユーザーがタップしたセルの種類によって遷移先の画面を振り分ける方法についてです。
例えば、UITableViewControllerに表示されるHogeCell1, HogeCell2, HogeCell3という3種類のカスタムセルがあり、それぞれの遷移先をHogeVC1, HogeVC2, HogeVC3とした場合、どのような処理を書けばよろしいでしょうか?
現在下記のようなコードを書いていますが、画面遷移が発生しないので
ご教授頂ければ幸いです。
よろしくお願いします。
※追記
下記のコードを試したのですが、let hogeCell1 = 〜のところで
「Thread1: signal SIGABRT」のエラーが出てしまいました。
どこが間違っているでしょうか?
Swift
1 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 2 3 let hogeCell1 = tableView.cellForRowAtIndexPath(indexPath) as! HogeCell1() 4 let hogeCell2 = tableView.cellForRowAtIndexPath(indexPath) as! HogeCell2() 5 let hogeCell3 = tableView.cellForRowAtIndexPath(indexPath) as! HogeCell3() 6 7 switch indexPath.row{ 8 case hogeCell1: 9 self.navigationController?.pushViewController(HogeVC1(), animated: true) 10 case hogeCell2: 11 self.navigationController?.pushViewController(HogeVC2(), animated: true) 12 case hogeCell3: 13 self.navigationController?.pushViewController(HogeVC3(), animated: true) 14 15 default: 16 break 17 18 } 19 20 } 21
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/10/13 13:55