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

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

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

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

Swift

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

Q&A

解決済

1回答

2110閲覧

Tableviewのセルをタップした時の画面遷移方法

LFOHP

総合スコア25

Xcode

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

Swift

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

0グッド

0クリップ

投稿2017/12/18 06:50

編集2017/12/18 07:26

打ち消し線###前提・実現したいこと
tableのセルをタップしたときに画面遷移(SecondView)させたいのですが、うまく行かないのです。

###発生している問題・エラーメッセージ

エラーメッセージ
ビルド自体は成功しますが、セルをタップしても画面遷移しません

###該当のソースコード class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { private let myItems: NSArray = ["TEST1", "TEST2", "TEST3","TEST4"] private var myTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height let displayWidth: CGFloat = self.view.frame.width let displayHeight: CGFloat = self.view.frame.height myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight)) myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") myTableView.dataSource = self myTableView.delegate = self self.view.addSubview(myTableView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { //print("Num: (indexPath.row)") //print("Value: (myItems[indexPath.row])") tableView.deselectRow(at: indexPath, animated: true) self.present(SecondViewController(), animated: true, completion: nil) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myItems.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) cell.textLabel!.text = "(myItems[indexPath.row])" return cell } } class SecondViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.white let backButton:UIButton = UIButton() backButton.frame = CGRect(x: 0, y: 0, width: 100, height: 100) backButton.setTitle("back", for: .normal) backButton.setTitleColor(UIColor.blue, for: .normal) backButton.layer.position = CGPoint(x: self.view.frame.width/2, y: self.view.frame.height/2) backButton.addTarget(self, action: #selector(back), for: .touchUpInside) self.view.addSubview(backButton) } @objc func back(){ self.dismiss(animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } class SecondViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.white let backButton:UIButton = UIButton() backButton.frame = CGRect(x: 0, y: 0, width: 100, height: 100) backButton.setTitle("back", for: .normal) backButton.setTitleColor(UIColor.blue, for: .normal) backButton.layer.position = CGPoint(x: self.view.frame.width/2, y: self.view.frame.height/2) backButton.addTarget(self, action: #selector(back), for: .touchUpInside) self.view.addSubview(backButton) } @objc func back(){ self.dismiss(animated: true, completion: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }

###補足情報(言語/FW/ツール等のバージョンなど)
Swift4

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

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

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

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

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

fuzzball

2017/12/18 07:06

「うまく行かないのです」を具体的に書いて下さい。
LFOHP

2017/12/18 07:13

TEST1のセルをタップしても、画面遷移しません
fuzzball

2017/12/18 07:22

「遷移しない」というのは、TableViewが表示されたままということでしょうか?tableView(_:didSelectRowAt:) は呼ばれているでしょうか?あと、コードが囲めていません。
LFOHP

2017/12/18 07:29

そうです。ableView(_:didSelectRowAt:) は呼ばれているか確認方法もわからないのですが。コードのエラーはないようなのですが。
fuzzball

2017/12/18 07:32 編集

print文のコメントを外せば呼ばれているかどうか分かるでしょう。
LFOHP

2017/12/18 07:35

何も出てこないので、呼ばれていない・・ようです。
LFOHP

2017/12/18 07:54

print("Num: \(indexPath.row)")がおかしいのでしょうか。
guest

回答1

0

ベストアンサー

tableView(_:didSelectRowAt:)がSwift2の書き方になってます。

swift

1override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

古い記事を参考にするときは気を付けて下さい。(Swift3以降の記事を推奨します)

投稿2017/12/18 08:07

fuzzball

総合スコア16731

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

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

LFOHP

2017/12/18 22:29

ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問