好吧,我被困住了。我在让自定义表格 View 单元格转到我的主从应用程序的“详细信息”部分时遇到了一个棘手的问题,并且 this other StackOverflow post修复了它,调用prepareForSegue函数。我仍然没有得到我期望的结果,并且我发现了一个大问题:将单元格的属性传递到详细信息部分需要选择一个单元格,并且该帖子中的代码会在转场之前立即取消选择该单元格。似乎是一个简单的修复,但是当我删除该行时,当我单击一个单元格时程序崩溃了。我真的不知道该怎么办(我的 iOS 体验 100% 来自过去 48 小时),希望这里有人可以提供帮助。
涉及代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = objects[indexPath.row]
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: false) // What appears to be the problem
performSegueWithIdentifier("showDetail", sender: cell)
}
错误(当我删除取消选择行时):
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
(在编辑器中会弹出:THREAD 1: EXC_BREAKPOINT)
感谢所有帮助。
最佳答案
你尝试输入这样的代码:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
performSegueWithIdentifier("showDetail", sender: cell)
tableView.deselectRowAtIndexPath(indexPath, animated: false) // What appears to be the problem
}
但我认为更好的是,您应该在 didSelectRowAtIndexpath
中获取要发送到下一个屏幕的对象,并将其传递给 performSegue
,如下所示:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let object = objects[indexPath.row]
performSegueWithIdentifier("showDetail", sender: object)
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let object = sender as! "type of object" {
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
希望这有帮助!
关于ios - 主详细信息应用程序indexPathForSelectedRow问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34457957/