How do I make multiple tableview cells to lead corresponding view controllers?

落爺英雄遲暮 提交于 2020-01-07 08:03:50

问题


I am making an app with a tableViewController as the initial view controller. It obviously has a list of items in tableview cells. I want each of the tableView cell to lead to a different View controller. Here is an example of my home screen: ![enter image description here][1]

What I am trying to achieve, for example, is that the tools cell will take me to a View controller with details about tools. And when I click on weapons, it should take me to a view controller with details about weapons. I already have the view controllers set up, but I don't know how to create segues to each view controller. NOTE: I made these cells in code and not in my storyboard. I made an array of items and I used indexpath.row to display them.

Thanks for any help!


回答1:


nstead of connecting and creating segue from individual cells, you can connect all those segues from the View Controller button, lying below your view in your storyboard. In this case, you will have multiple segues and none of them will be individually connected to cells. And when segues are ready, you can use this method for moving on to the next View Controller depending on which cell is tapped from the tableView.

[self performSegueWithIdentifier:@"yourSegue" sender:nil];

You just have to check which cell is tapped and then perform the appropriate Segue on tap.

Look out for the yellow button as shown below your View Screen in your storyboard.

Drag and drop a segue from that button onto the View Controller that you want to connect.

Hope this helps.




回答2:


This was extremely helpful and what I needed to do to get rid of the segue from the table view cell. Doing exactly what you suggested above and adding this to the tableViewDidSelectRowAtIndexPath...

    override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) 
    {

      if let indexPath = self.tableView.indexPathForSelectedRow
      {

        rowSelected = indexPath.row
        sectionSelected = indexPath.section

        //Transition to the Set Details VC
        if indexPath.section == 0
        {
            print("Set Details")
            self.performSegueWithIdentifier("Segue1", sender: self)
        }

        //Transition to the Info Section
        else if indexPath.row == 0 && indexPath.section == 2
        {
            print("About Page")
            self.performSegueWithIdentifier("Segue2", sender: self)
        }

        }//End If IndexPath

    }//End DSRAIP


来源:https://stackoverflow.com/questions/24318781/how-do-i-make-multiple-tableview-cells-to-lead-corresponding-view-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!