Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

ios - prepareForSegue not called from custom uitableviewcell

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?) {
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...