打ち消し線###前提・実現したいこと
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
コードは ``` で囲って下さい。 https://teratail.com/help#about-markdown
「うまく行かないのです」を具体的に書いて下さい。
TEST1のセルをタップしても、画面遷移しません
「遷移しない」というのは、TableViewが表示されたままということでしょうか?tableView(_:didSelectRowAt:) は呼ばれているでしょうか?あと、コードが囲めていません。
そうです。ableView(_:didSelectRowAt:) は呼ばれているか確認方法もわからないのですが。コードのエラーはないようなのですが。
print文のコメントを外せば呼ばれているかどうか分かるでしょう。
何も出てこないので、呼ばれていない・・ようです。
print("Num: \(indexPath.row)")がおかしいのでしょうか。
回答1件
あなたの回答
tips
プレビュー