1

My tableview currently fills an array of objects from core data and displays them. This part of the application works perfectly. I can add objects, scroll through them and delete them:

var items : [Item] = []

What I'm struggling with is passing the object of the selected cell to another view controller. I want to implement an edit function so that users can edit their items. When an item in the tableview is tapped, a segue to a new "Edit" view controller should occur. How can I pass the selected item in the tableview to a new view controller?

Spencer
  • 95
  • 2
  • 5

1 Answers1

2

Just grab the right item in tableView(:didSelectRowAtIndexPath:) and pass it to the view controller, then perform the segue:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
    // Setup the detail view controller with the item to display
    // It should be defined as `var item: Item!` in DetailViewController
    let detailViewController = DetailViewController()
    detailViewController.item = items[indexPath.row]

    // This will perform the segue you define in IB, with the view controller pre-loaded and configured
    detailViewController.performSegueWithIdentifier("detailSegue", sender: self)
}
gskbyte
  • 467
  • 3
  • 13