继续使用 didSelectRowAtIndexPath

Segue using didSelectRowAtIndexPath

我有一个 Core Data 项目的主从 VC 设置。如果 searchBar 处于活动状态,则显示一组结果,如果它不活动,则来自 fetchedResultsController 的对象显示在 MasterVC.

我一直在尝试使用 prepareForSegue 进行 segue,但我的导师建议我使用 didSelectRowAtIndexPath 进行 segue。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var selectedNote: Note

    // Check to see which table view cell was selected.
    if tableView == self.tableView {
        selectedNote = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Note // <--this is "everything"
    } else {
        // need this to unwrap the optional
        if let filteredObjects = filteredObjects {
            selectedNote = filteredObjects[indexPath.row]
        }
    }

    // Set up the detail view controller to show.
    let detailViewController = DetailViewController()
    detailViewController.detailDescriptionLabel = selectedNote.valueForKey("noteBody") as! UILabel

    // Note: Should not be necessary but current iOS 8.0 bug requires it.
    tableView.deselectRowAtIndexPath(tableView.indexPathForSelectedRow()!, animated: false)

    // original code
    navigationController?.pushViewController(detailViewController, animated: true)

}

我遇到这个编译器错误:

Variable 'selectedNote' used before being initialized--在方法的顶部声明!

如果我像这样在 selectedNote 之前添加 "self"

    detailViewController.detailDescriptionLabel = self.selectedNote.valueForKey("noteBody") as! UILabel

'MasterViewController' does not have a member named 'selectedNote' 尽管在那里。所以我显然搞砸了一些东西。

我在 let detailViewController = DetailViewController() 之前和 lldb 中放置了一个断点,它正在打印出正确的对象。我在这里四处寻找解决方案,但我做空了。我找不到适用于 GitHub.

的适用代码
class Note: NSManagedObject {

    @NSManaged var dateCreated: NSDate
    @NSManaged var dateEdited: NSDate
    @NSManaged var noteTitle: String
    @NSManaged var noteBody: String

}

知道如何将 selectedNote 的属性转发给 detailViewController 吗?

更新:

根据我得到的回复,我已经用这段代码关闭了编译器警告:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var selectedNote: Note?

    // Check to see which table view cell was selected.
    if tableView == self.tableView {
        selectedNote = self.fetchedResultsController.objectAtIndexPath(indexPath) as? Note // <--this is "everything"
    } else {
        // need this to unwrap the optional
        if let filteredObjects = filteredObjects {
            selectedNote = filteredObjects[indexPath.row]
        }
    }

    // Set up the detail view controller to show.
    let detailViewController = DetailViewController()

    detailViewController.detailDescriptionLabel.text = (selectedNote!.valueForKey("noteBody") as! String)

    // Note: Should not be necessary but current iOS 8.0 bug requires it.
    tableView.deselectRowAtIndexPath(tableView.indexPathForSelectedRow()!, animated: false)

    // original code
    navigationController?.pushViewController(detailViewController, animated: true)

}

但是当它崩溃时,我在控制台中得到了这个: There are already notes in the app fatal error: unexpectedly found nil while unwrapping an Optional value

但是,当我键入 po selectedObject 时,我单击的对象会显示在控制台中。

您需要将 selectedNote 声明为可选,如下所示:

  var selectedNote: Note?

然后在使用之前检查值是否存在。

  if let note = selectedNote {
    // Set up the detail view controller to show.
    let detailViewController = DetailViewController()
    detailViewController.detailDescriptionLabel = note.valueForKey("noteBody") as! UILabel

    // Note: Should not be necessary but current iOS 8.0 bug requires it.
    tableView.deselectRowAtIndexPath(tableView.indexPathForSelectedRow()!, animated: false)            
    // original code
    navigationController?.pushViewController(detailViewController, animated: true)
  }

更新

问题是您正在尝试创建 DetailViewController

let detailViewController = DetailViewController()

但是您需要的是引用 DetailViewController 以便将信息传递给它。

因此您可以在界面生成器中创建从主控制器到细节控制器的转场。然后从 didSelectRowAtIndexPath

中删除逻辑
 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    // Note: Should not be necessary but current iOS 8.0 bug requires it.
    tableView.deselectRowAtIndexPath(tableView.indexPathForSelectedRow()!, animated: false)
}

并在prepareForSegue方法中实现:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showDetail" {

        if let cell = sender as? UITableViewCell {

            let indexPath = tableView.indexPathForCell(cell)!

            var selectedNote: Note?

            if filteredObjects?.count > 0 {
                selectedNote = filteredObjects![indexPath.row]
            }else {
                selectedNote = self.fetchedResultsController.objectAtIndexPath(indexPath) as? Note // <--this is "everything"
            }

            if let note = selectedNote {

                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = note
            }
        }
    }
}

showDetail - 您需要在 IB 中设置的 segue 标识符。 var detailItem:AnyObject? - 你需要在 DetailViewController 中声明它。