简体   繁体   中英

How to show new View Controller when a Table View Cell is clicked in Swift

I am trying to show a new view controller when a table view cell is clicked. But my problem is when I click table view cell the new view controller does not appear. I already have looked at solutions on SO but it didn't work.

Here is a screenshot of storyboard在此处输入图片说明

And here is the relevant code:

import UIKit

class RestaurantTableViewController: UITableViewController {

    let cellIdentifier: String = "restaurantCell"


    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "restaurantSegue" {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let destination = storyboard.instantiateViewController(withIdentifier: "RestaurantInfoViewController") as! RestaurantInfoViewController
            navigationController?.pushViewController(destination, animated: true)
        }
    }
}

extension RestaurantTableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell

        return cell
    }

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "restaurantSegue", sender: self)
    }

}

Note: i also followed Apple's documentation in the link Apple's guide . But this didn't work as well

According to the screenshot the segue is connected to the table view cell.

If so delete didSelectRowAt because the segue is performed directly

 
 
  
   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.performSegue(withIdentifier: "restaurantSegue", sender: self) }
 
 

The second issue is if you are using a segue you must not call instantiateViewController .

If you don't have to pass data to the next view controller you can even delete

 
 
  
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "restaurantSegue" { let storyboard = UIStoryboard(name: "Main", bundle: nil) let destination = storyboard.instantiateViewController(withIdentifier: "RestaurantInfoViewController") as! RestaurantInfoViewController navigationController?.pushViewController(destination, animated: true) } }
 
 

Otherwise get the destination controller from the segue in prepare(for and use that.

If the segue is still not performed then there's something wrong with the identifier.

You just need to make the segue.destination as your viewcontroller, no need to push it. The performSegue method will take care of it. Like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "restaurantSegue" {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            segue.destination =  storyboard.instantiateViewController(withIdentifier: "RestaurantInfoViewController") as! RestaurantInfoViewController

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM