0

I have a table view embedded in my view controller, and I want to be able to setup segues to four other view controllers depending on what cell in the view controller is clicked.

However, I can't seem to figure out how to control+drag a segue from the table view to a view controller.

I believe the relevant section of code would be:

    import UIKit

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    let options = ["Home", "My Account", "Settings", "Support"]

    //MARK: Properties
    var selectedItem: String?
    @IBOutlet weak var nameTextField: UILabel!

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
        cell.textLabel?.text = options[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return options.count
    }

    //MARK: Actions
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let selectedRow = tableView.cellForRow(at: indexPath){
            selectedItem = selectedRow.textLabel?.text
        }

        if selectedItem == "My Account"{
            performSegue(withIdentifier: "classifySegue", sender: self)
        } else if selectedItem == "Settings"{
            performSegue(withIdentifier: "otherSegue", sender: self)
        }

    }


}

This would be the view and storyboard: Simulation Storyboard

The problem is the current otherSegue comes from the View Controller and not the Table View itself.

user3628240
  • 877
  • 1
  • 23
  • 41
  • https://stackoverflow.com/a/41887007/5461400 – Harshal Valanda Sep 14 '17 at 05:10
  • do you mean you want to push from the tableview.view only and not from the parent viewcontroller of the tableview? – dhin Sep 14 '17 at 05:25
  • Yes, I just want to make it so when I click on a row in the table view, depending on the text in the row, it will perform a specific segue to a view controller that I specify. – user3628240 Sep 14 '17 at 05:26

2 Answers2

0

It doesn't need to come from the tableView itself. The segue should come from the VC where the tableView is contained.

Your code would still work since your

performSegue(withIdentifier: "classifySegue", sender: self)

is invoking a perform segue from self which is your ViewController

NOTE: In your didSelect function, you don't need to get the cell again, just set your selectedItem to self.options[indexPath.row] since you're only trying to get the text that you pass in the cell initially.

Ace Rivera
  • 620
  • 4
  • 14
  • Do you know why when I click on a cell in the simulation, it doesn't work? I added in the rest of my code for the view controller page, but for some reason the cell doesn't link to the next view controller. – user3628240 Sep 14 '17 at 05:10
  • have you tried to check the value of selectedItem using breakpoints? are they correct? and have you made the segue from VC to the next ViewController as I mentioned? – Ace Rivera Sep 14 '17 at 05:34
  • I tried to just print("Hello") to the didSelectRow and nothing was printed to the console, so somehow it seems like the function isn't even being called when I click on a row. Putting an override in front of it gave me an error of not overriding any method from it superclass. Is it because I named my tableView "tableView" instead of something else? – user3628240 Sep 14 '17 at 05:37
  • you have to connect your tableView delegate and dataSource to your ViewController in storyboard. – Ace Rivera Sep 14 '17 at 05:45
  • Ok, thanks! That helped a lot! Why did doing selectedItem = self.options[indexPath.row] work, but the previous if statement setting it didn't work? – user3628240 Sep 14 '17 at 05:53
  • I think it's because the cell.textLabel? is optional, and if your try to print the cell.textLabel?.text, it's either there is no value or it will show something like, "(Optional)someStringHere" – Ace Rivera Sep 14 '17 at 06:08
0

StoryBoard is not allow segue from tableview because it is not clickable/ have't any action ,So You can make segue from tableView Cell.

Nauman Malik
  • 1,326
  • 9
  • 27