主从应用程序 indexPathForSelectedRow 问题
Master Detail Application indexPathForSelectedRow issue
好吧,我卡住了。我在让我的自定义 table 视图单元格转到我的 Master-Detail 应用程序的 'details' 部分时遇到了一个棘手的问题,并且 this other Whosebug post 修复了它,调用了 prepareForSegue 函数。我仍然没有得到我期望的结果,而且我发现了一个大问题:将单元格的属性传递到详细信息部分需要选择一个单元格,并且 post 中的代码取消选择紧接在继续。似乎是一个简单的修复,但是当我删除该行时,当我单击一个单元格时程序崩溃了。我真的不知道在这里做什么(我的 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)
(在编辑器中弹出:线程 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
}
}
}
希望对您有所帮助!
好吧,我卡住了。我在让我的自定义 table 视图单元格转到我的 Master-Detail 应用程序的 'details' 部分时遇到了一个棘手的问题,并且 this other Whosebug post 修复了它,调用了 prepareForSegue 函数。我仍然没有得到我期望的结果,而且我发现了一个大问题:将单元格的属性传递到详细信息部分需要选择一个单元格,并且 post 中的代码取消选择紧接在继续。似乎是一个简单的修复,但是当我删除该行时,当我单击一个单元格时程序崩溃了。我真的不知道在这里做什么(我的 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)
(在编辑器中弹出:线程 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
}
}
}
希望对您有所帮助!