Reputation: 155
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.
Upvotes: 2
Views: 1108
Reputation: 10479
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
}
}
}
Upvotes: 1