3

I was following this tutorial http://www.raywenderlich.com/76519/add-table-view-search-swift when I ran into an error. I am adding this feature into an app I was already working on. Once I am in the booths table view, I want to be able to navigate out into the main menu with a button on the navigation bar. Here is the section of code that deals with the segues.

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("BoothDetail", sender: tableView)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "BoothDetail" {
        let BoothDetailViewController = segue.destinationViewController as UIViewController
        if sender as UITableView == self.searchDisplayController!.searchResultsTableView {
            let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
            let destinationTitle = self.filteredBooths[indexPath.row].name
            BoothDetailViewController.title = destinationTitle
        } else {
            let indexPath = self.tableView.indexPathForSelectedRow()!
            let destinationTitle = self.booths[indexPath.row].name
            BoothDetailViewController.title = destinationTitle
        }
    }
}

}

The error is thrown while trying to use the back button on the booths list that is a direct show segue to the main conference menu. The error is on this line.

    if sender as UITableView == self.searchDisplayController!.searchResultsTableView {
user3558131
  • 113
  • 1
  • 7

2 Answers2

1

You have quite a few problems. Some fatal, some just a headache.

the first headache is you are calling the same segue twice. Both functions call the same segue. Both will execute. Now if you want a double animation, okay. But since one passes data and the other does not, you may have an issue. Eliminate the didSelectRowAtIndexPath function.

In your prepareForSegue method it appears you have two different objects connected to the same segue. A searchDisplayController and a tableView. You want two separate segues. Then your if/else makes changes based on which segue was chosen:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "segue1" {

            //code set 1

        } else if segue.identifier == "segue2" {

            //code set 2
        }
    }
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
-1

I had similar problem and I just got it solved. When creating your segue in the tableview do not drag it from the cell, create a manual segue called "BoothDetail" and connect it to BoothDetailViewController, to create a manual segue select the table view controller and click on "show connection inspector" you will see manual under triggered segue .

My problem the sender was a tableviewcell and not the tableview, so the function

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.performSegueWithIdentifier("BoothDetail", sender: tableView) }

was never called to pass the tableview controller so when you try to cast it you were getting the error.

good luck

Ali
  • 31
  • 1
  • 3