Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigate to a new view controller for every cell in table view

Tags:

xcode

swift

I am a beginner coder in swift. I am trying to create a tabbed application. For one of my tabs, I am creating a table view which has multiple rows each which have a different task (A good way to think of this is the facebook app, where each option in the more screen will take you to a separate view)

Now, my table is populated with an array:

let array  = ["one", "two", "three]

I want to ask, that everytime that I tap on one of these rows, I would like to go a new view controller. How is this possible?

What I tried was performSegue with an identifier which I give in the storyboard, but then there would be an x amount of segues connecting to the table view? So I don't think this is right? :/

I know the contents of the array prior to generating the table, so If I know the array value, and the row being tapped, how can I navigate to a new view controller?

Edit:

When performing the segue between the controllers, I am using:

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

This of course will only connect to the segue showView however, how can i add multiple view controllers?

like image 410
Nouman Avatar asked Feb 13 '17 01:02

Nouman


People also ask

How do I switch between view controllers?

From the Library, add a button to the first View Controller and name the button, for example: Show Second View . Select the button, press and hold the Control key on the keyboard and drag from the button to the second View Controller.

How do I add a view controller to my navigation controller?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .

How do you get the Indexpath row when a button in a cell is tapped?

You can walk up the view hierarchy to find the containing table view cell and the containing table view. Then you can ask the table view for the cell's index path. Then use it to find the index path like this: func addButtonTapped(_ sender: Any) { guard let button = sender as?

How to navigate from one view controller to another view controller?

To navigate from one view Controller to another view Controller in iOS, we need to use Navigation controller. Navigation controller manages a stack of View controller when we go from one view to another view. Navigation from one view controller to another view controller can be done like mentioned below. Step 1 − Create a View controller object.

What is the implementation of Tableview cellforrowatindexpath?

The implementation of tableView:cellForRowAtIndexPath: is similar to the one we saw in the previous article. The main difference is how we fetch the data that we display in the table view cell.

Is it possible to segue from one view controller to another?

You can also do this on dynamic cells if you don't need to pass any data to the next View Controller. However, if you do need to pass data then you should make the segue from the Table View's view controller itself and not the cell.

How to push the books view controller onto the navigation stack?

To push the books view controller onto the navigation stack of the navigation controller, we need to create another segue. This time, however, we need to create a manuel segue, a push segue to be precise.


2 Answers

You need to simply compare which row is selected in tableView and then perform segue according to it.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let segueIdentifier: String
    switch indexPath.row {
    case 0: //For "one"
         segueIdentifier = "showView1"
    case 1: //For "two"
         segueIdentifier = "showView2"
    default: //For "three"
         segueIdentifier = "showView3"
    }
    self.performSegue(withIdentifier: segueIdentifier, sender: self)
}
like image 85
Nirav D Avatar answered Sep 19 '22 11:09

Nirav D


Add the following function to your controller.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    navigationController?.pushViewController(NewController(), animated: true)
}
like image 25
javimuu Avatar answered Sep 19 '22 11:09

javimuu