You must use the didSelectRowAtIndexPath method to determine if a cell has been selected.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { self.performSegueWithIdentifier("showQuestionnaire", sender: indexPath); }
Then in your prepareForSegue method
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { if (segue.identifier == "showQuestionnaire") { let controller = (segue.destinationViewController as! UINavigationController).topViewController as! QuestionnaireController let row = (sender as! NSIndexPath).row;
To explain ...
- I used the index path as the sender, so I can easily go through the index path. You can also check the selected cell using other UITableView methods, but I always did this with success.
- You cannot put
performSegueWithIdentifier in the preparation method for the segue method, because performSegueWithIdentifier leads to prepareForSegue ; You just loop around and without purpose. (When you want to execute segue, prepareForSegue is always executed) prepareForSegue does not start by itself when a row is selected. Here you need didSelectRowAtIndexPath . You need to performSegueWithIdentifier outside the method, as described above, which should be in didSelectRowAtIndexPath
source share