0

Well, I'm stuck. I was having a tricky problem with getting my custom table view cell to segue to the 'details' portion of my Master-Detail application, and this other StackOverflow post fixed it, calling the prepareForSegue function. I still wasn't getting the results I was expecting, and I found a big problem: passing the cell's attributes to the detail section requires a cell to be selected, and the code from that post deselects the cell immediately before the segue. Seems like an easy fix, but when I removed that line the program crashed when I clicked on a cell. I don't really know what to do here (100% of my iOS experience is from the last 48 hours), and am hoping someone here can help.

Code Involved:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = objects[indexPath.row]
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.detailItem = object
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    tableView.deselectRowAtIndexPath(indexPath, animated: false) // What appears to be the problem
    performSegueWithIdentifier("showDetail", sender: cell)
}

Error (When I remove the deselect line):

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

(In the editor this pops up: THREAD 1: EXC_BREAKPOINT)

All help is appreciated.

Community
  • 1
  • 1
Carl Litchman
  • 129
  • 2
  • 10
  • If the segue is connected to the table view cell (rather than the controller) `prepareForSegue` is called automatically. You could deselect the cell in `didSelectRowAtIndexPath` but you must not call `performSegueWithIdentifier` explicitly. – vadian Dec 24 '15 at 21:21

1 Answers1

0

You try put code like this:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    performSegueWithIdentifier("showDetail", sender: cell)
    tableView.deselectRowAtIndexPath(indexPath, animated: false) // What appears to be the problem
}

But I think better you should get object you want send to next screen in didSelectRowAtIndexpath and pass it to performSegue like this:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     let object = objects[indexPath.row]
     performSegueWithIdentifier("showDetail", sender: object)
     tableView.deselectRowAtIndexPath(indexPath, animated: false) 
}


 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let object = sender as! "type of object" {

            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.detailItem = object
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
       }
   }
}

Hope this help!

vien vu
  • 4,277
  • 2
  • 17
  • 30