5
votes

I have a custom TableViewController with a custom TableViewCell. I created a segue on the storyboard from the Cell to another ViewController to display the details but prepareForSegue is never called. I've tried using didSelectRowAtIndexPath but its not called either. I suspect it may be because I create the custom cells dynamically and they don't get the segue from the storyboard assigned to them, but I couldn't find a way to do so. 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 I might be doing something wrong when 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?

1
Did you set the controller where this code resides as the delegate of the table view (it should be set automatically if your class is a subclass of UITableViewController)? Do you have views in the cell that might be intercepting the touches? - rdelmar
Yes, I set it as a delegate. The cell has its custom view, but im not able to create a segue from the view nor the labels. - filipebarretto
You have to connect from the cell itself, not the content view or labels. If you can't grab the cell from the canvas, then go to the scene list on the left, and drag from the cell there. - rdelmar
That is exactly what I did. I connected from the cell, but the segue isn't firing. I though about attaching a tap gesture event to my cell class but I don't believe that is the appropriate solution. - filipebarretto
It sounds like you've set it up correctly, so it's hard to say what's wrong without actually seeing your project. If you can post it somewhere or email it to me, I'd be glad to take a look at it. - rdelmar

1 Answers

3
votes

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

Also check the function signatures. The exclamation marks for implicitly unwrapped optionals 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?) {
}