videoperson
videoperson

Reputation: 139

Swift selecting rows in TableViewController and opening up a ViewController

I started learning App development in Swift recently and I'm still fairly new. I'm trying to make an app where at the home screen, the user will click a button called profiles, and it'll show a list of names in a tableViewController. When the user clicks a name it'll open up the person's profile in a viewController.

I'm stuck so far on what method and what to code in that method to allow the user to click on the list of names in my TableViewController and then open up a viewController for that person. I've already created my other viewController (DetailsViewController) and created a segue between the TableViewController and the DetailsViewController.

I've been searching for a long time on how to do this but couldn't find any luck. If someone could just point me in the right direction that'd be great.

Upvotes: 0

Views: 1065

Answers (1)

Mundi
Mundi

Reputation: 80265

You have to create the segue from the cell to the detail view controller. To provide the detail controller with the data, give it a property (e.g. name) that you can set in prepareForSegue which you override in the parent controller.

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
   let cell = sender as UITableViewCell // or your cell subclass
   let indexPath = self.tableView.indexPathForCell(cell)
   let name = dataArray[indexPath.row]
   // or something else depending on the structure of your data model
   let controller = segue!.destinationViewController() as DetailViewController
   controller.name = name
}

The detail view controller can now populate its view with the passed information.

Upvotes: 1

Related Questions