prepareForSegue未从自定义uitableviewcell调用 [英] prepareForSegue not called from custom uitableviewcell
问题描述
我有一个带有自定义TableViewCell的自定义TableViewController。我在故事板上创建了一个segue,从Cell到另一个ViewController来显示细节,但是从未调用prepareForSegue。我已经尝试过使用didSelectRowAtIndexPath但它没有被调用。我怀疑这可能是因为我动态创建自定义单元格并且它们没有从分配给它们的故事板中获取segue,但我找不到这样做的方法。我的BarButtonItem中的newSegue被正常调用。
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怀疑我在定义自定义单元格时可能做错了什么:
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
}
任何帮助?
推荐答案
从InterfaceBuilder中的TableViewController拖动segue,而不是从单元格拖动。然后你可以在 didSelectRowAtIndexPath
中通过 performSegueWithIdentifier
执行带有标识符的segue。
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?) {
}
这篇关于prepareForSegue未从自定义uitableviewcell调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!