2

The code in question:

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

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    var card: String

    if (tableView == self.searchDisplayController?.searchResultsTableView){
        card = self.filtered[indexPath.row]
    }

    else{
        card = self.array[indexPath.row]
    }



    let destinationVC = SearchViewController()
    destinationVC.searchString = card
    destinationVC.performSegueWithIdentifier("ResultSegue", sender: self)

}

I'm trying to pass a string to another view when a cell in my table is selected. In the storyboard I named the segue identifier 'ResultSegue'.

What happens when I run my app is when I click a cell it loads the next view, without the variable I'm sending being updated, then if I go back and click on a new cell the app will crash and give me the titled warning.

I've tried running clean, and resetting the simulator as other threads have suggested, but nothing changes.

Chris
  • 155
  • 1
  • 9
  • Did you mean to implement didSelectRow, not didDeselectRow? – rdelmar Jun 07 '15 at 00:47
  • http://stackoverflow.com/questions/26207846/pass-data-through-segue here is a SO question that i think can help you! show's you how to pass the data. I also agree with @rdelmar , this is in the wrong function. – Jay Jun 07 '15 at 04:06
  • @Jay Yeah, I fixed the function to the right one. I used to link you posted to get the code I have now, someone in the comments had the same problem I'm having, but they didn't specify how they resolved it – Chris Jun 07 '15 at 05:08

1 Answers1

1

You should pass a the string in prepareForSegue function rather than didSelectRowAtIndexPath function.

Instead of using didSelectRowAtIndexPath as below:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ResultSegue" {

        if let indexPath = self.tableView.indexPathForSelectedRow() {
            var card: String
            if (tableView == self.searchDisplayController?.searchResultsTableView) {
                card = self.filtered[indexPath.row]
            } else {
                card = self.array[indexPath.row]
            }
            (segue.destinationViewController as SearchViewController).searchString = card
        }
    }
}
Bannings
  • 10,376
  • 7
  • 44
  • 54