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












3















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?










share|improve this question





























    3















    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?










    share|improve this question



























      3












      3








      3


      2






      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?










      share|improve this question
















      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?







      swift xcode






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 1 '18 at 15:48







      Nouman

















      asked Feb 13 '17 at 1:16









      NoumanNouman

      92212




      92212
























          3 Answers
          3






          active

          oldest

          votes


















          6














          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)
          }





          share|improve this answer
























          • Perfect! Coming from a php background I should've thought of this exactly! But not really knowing syntax gets me confused. Thanks loads!!

            – Nouman
            Feb 13 '17 at 19:34











          • @Nouman Welcome mate :)

            – Nirav D
            Feb 14 '17 at 4:20



















          1














          Add the following function to your controller.



          override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          navigationController?.pushViewController(NewController(), animated: true)
          }





          share|improve this answer

































            1














            Using Static Cells in a UITableViewController



            Summary



            An alternative is to use a UITableViewController with static cells. If you know already know the menu then you can just create static cells for each item.



            In the storyboard you can connect a segue from each cell to their respective view controllers.



            Example



            example






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42195290%2fnavigate-to-a-new-view-controller-for-every-cell-in-table-view%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6














              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)
              }





              share|improve this answer
























              • Perfect! Coming from a php background I should've thought of this exactly! But not really knowing syntax gets me confused. Thanks loads!!

                – Nouman
                Feb 13 '17 at 19:34











              • @Nouman Welcome mate :)

                – Nirav D
                Feb 14 '17 at 4:20
















              6














              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)
              }





              share|improve this answer
























              • Perfect! Coming from a php background I should've thought of this exactly! But not really knowing syntax gets me confused. Thanks loads!!

                – Nouman
                Feb 13 '17 at 19:34











              • @Nouman Welcome mate :)

                – Nirav D
                Feb 14 '17 at 4:20














              6












              6








              6







              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)
              }





              share|improve this answer













              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)
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 13 '17 at 4:37









              Nirav DNirav D

              55.2k998136




              55.2k998136













              • Perfect! Coming from a php background I should've thought of this exactly! But not really knowing syntax gets me confused. Thanks loads!!

                – Nouman
                Feb 13 '17 at 19:34











              • @Nouman Welcome mate :)

                – Nirav D
                Feb 14 '17 at 4:20



















              • Perfect! Coming from a php background I should've thought of this exactly! But not really knowing syntax gets me confused. Thanks loads!!

                – Nouman
                Feb 13 '17 at 19:34











              • @Nouman Welcome mate :)

                – Nirav D
                Feb 14 '17 at 4:20

















              Perfect! Coming from a php background I should've thought of this exactly! But not really knowing syntax gets me confused. Thanks loads!!

              – Nouman
              Feb 13 '17 at 19:34





              Perfect! Coming from a php background I should've thought of this exactly! But not really knowing syntax gets me confused. Thanks loads!!

              – Nouman
              Feb 13 '17 at 19:34













              @Nouman Welcome mate :)

              – Nirav D
              Feb 14 '17 at 4:20





              @Nouman Welcome mate :)

              – Nirav D
              Feb 14 '17 at 4:20













              1














              Add the following function to your controller.



              override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
              navigationController?.pushViewController(NewController(), animated: true)
              }





              share|improve this answer






























                1














                Add the following function to your controller.



                override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                navigationController?.pushViewController(NewController(), animated: true)
                }





                share|improve this answer




























                  1












                  1








                  1







                  Add the following function to your controller.



                  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                  navigationController?.pushViewController(NewController(), animated: true)
                  }





                  share|improve this answer















                  Add the following function to your controller.



                  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                  navigationController?.pushViewController(NewController(), animated: true)
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 13 '17 at 1:54









                  Obsidian Age

                  27k72344




                  27k72344










                  answered Feb 13 '17 at 1:32









                  javimuujavimuu

                  1,253824




                  1,253824























                      1














                      Using Static Cells in a UITableViewController



                      Summary



                      An alternative is to use a UITableViewController with static cells. If you know already know the menu then you can just create static cells for each item.



                      In the storyboard you can connect a segue from each cell to their respective view controllers.



                      Example



                      example






                      share|improve this answer




























                        1














                        Using Static Cells in a UITableViewController



                        Summary



                        An alternative is to use a UITableViewController with static cells. If you know already know the menu then you can just create static cells for each item.



                        In the storyboard you can connect a segue from each cell to their respective view controllers.



                        Example



                        example






                        share|improve this answer


























                          1












                          1








                          1







                          Using Static Cells in a UITableViewController



                          Summary



                          An alternative is to use a UITableViewController with static cells. If you know already know the menu then you can just create static cells for each item.



                          In the storyboard you can connect a segue from each cell to their respective view controllers.



                          Example



                          example






                          share|improve this answer













                          Using Static Cells in a UITableViewController



                          Summary



                          An alternative is to use a UITableViewController with static cells. If you know already know the menu then you can just create static cells for each item.



                          In the storyboard you can connect a segue from each cell to their respective view controllers.



                          Example



                          example







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 4:03









                          Mark MoeykensMark Moeykens

                          3,66812828




                          3,66812828






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42195290%2fnavigate-to-a-new-view-controller-for-every-cell-in-table-view%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Full-time equivalent

                              さくらももこ

                              13 indicted, 8 arrested in Calif. drug cartel investigation