5

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?

filipebarretto
  • 1,842
  • 1
  • 13
  • 27
  • 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 Oct 07 '14 at 16:06
  • 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 Oct 07 '14 at 16:52
  • 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 Oct 07 '14 at 18:33
  • 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 Oct 07 '14 at 20:02
  • 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 Oct 07 '14 at 20:39
  • Thanks! I'll set up a project with just this issue and send it! If it helps, the tableviewcell isnt being selected on click, thats another reason I suspect its something with the custom cell class. – filipebarretto Oct 09 '14 at 12:40
  • Well, I restarted the project with doing the same things and it works. I really don't understand why. Now I'm facing a different problem: I have a text view in the cell and the segue isn't called when I click on the text view. – filipebarretto Nov 17 '14 at 14:58
  • I am having the same problem. Could you fix it ? @filipebarretto – Utku Dalmaz Apr 25 '16 at 09:25

1 Answers1

3

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?) {
}
zisoft
  • 22,770
  • 10
  • 62
  • 73
  • 1
    This is a work around, not a solution to the OP's problem. This won't work any way, since the OP said that didSelectRowAtIndexPath is not being called. – rdelmar Oct 07 '14 at 16:03
  • I've tried this aproach also, but, as I sair, the didSelectRowAtIndexPath is not being called. The cell is defined at MyTableViewCell.swift. I don't know if this could be causing the problem. – filipebarretto Oct 07 '14 at 16:49
  • Your `didSelectRowAtIndexPath` signature is incorrect, thus it is not called. Check my sample code above. You have to remove the exclamation marks. The code signature has changed between the beta phase of Xcode and the automatic code completion is still incorrect. – zisoft Oct 08 '14 at 07:22
  • I removed the exclamation marks and it's still not being called. I suspect i might be missing something when defining my custom cell. I'll add the code to my original question. – filipebarretto Oct 08 '14 at 23:29
  • The same for `prepareForSeque`: Remove the exclamation mark. I have updated my answer above. – zisoft Oct 09 '14 at 06:43
  • I am having the same problem and this didn't work for me – Utku Dalmaz Apr 25 '16 at 09:28
  • I'm having the same problem as well and this didn't work for me. – Number1 Feb 13 '17 at 03:09