PrepareForSegue not called from user uitableviewcell

I have my own TableViewController with a custom TableViewCell. I created a segue on a storyboard from Cell for another ViewController to display the details, but prepareForSegue is never called. I tried using didSelectRowAtIndexPath, but it was not called. I suspect this may be due to the fact that I create custom cells dynamically and they do not get the indent from the storyboard assigned to them, but I could not find a way to do this. The "NewSegue" from my BarButtonItem is called normally.

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { println("PREPARE FOR SEGUE") if segue.identifier == "newSegue" { println("PREPARE FOR NEW SEGUE") } else if segue.identifier == "detailSegue" { println("PREPARE FOR DETAIL SEGUE") } } override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { println("You selected cell!") } 

I suspect that maybe I'm wrong in defining my custom cell:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let CellIndentifier: NSString = "ListPrototypeCell" var cell : MyTableViewCell = tableView.dequeueReusableCellWithIdentifier(CellIndentifier) as MyTableViewCell var myClass: MyClass = self.myList.objectAtIndex(indexPath.row) as MyClass cell.setCell(author: myClass.author, message: myClass.message) return cell } 

Any help?

+5
source share
1 answer

Drag the segment from the TableViewController to the InterfaceBuilder, not from the cell. Then you can execute segue with its identifier in didSelectRowAtIndexPath via performSegueWithIdentifier .

Also check function signatures. Exclamation marks for implicitly expanded options are no longer needed:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) tableView.deselectRowAtIndexPath(indexPath, animated: true) performSegueWithIdentifier("mySegue", sender: cell) } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { } 
+3
source

Source: https://habr.com/ru/post/1204177/


All Articles