Master Detail应用程序索引SelectedRow问题的路径



好吧,我被卡住了。我遇到了一个棘手的问题,要让我的自定义表视图单元格分段到Master-Detail应用程序的"details"部分,而另一篇StackOverflow帖子通过调用prepareForSegue函数解决了这个问题。我仍然没有得到预期的结果,我发现了一个大问题:将单元格的属性传递到详细信息部分需要选择一个单元格,而该帖子中的代码会在segue之前取消选择该单元格。这似乎是一个简单的修复方法,但当我删除那一行时,当我点击一个单元格时,程序崩溃了。我真的不知道在这里该怎么办(我的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
       }
   }
}

希望得到帮助!

最新更新