使用didSelectRowAtIndexPath进行搜索

时间:2015-03-01 18:01:38

标签: ios swift core-data segue didselectrowatindexpath

我有一个核心数据项目的Master-Detail VC设置。如果searchBar处于活动状态,则会显示一组结果,如果它未激活,则fetchedResultsController中的对象将显示在MasterVC中。

我一直试图使用prepareForSegue进行调整,但我的导师建议我使用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 - 它已在方法的顶部声明!

如果我在"self"之前添加selectedNote,请执行以下操作:

    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时,我单击的对象会显示在控制台中。

1 个答案:

答案 0 :(得分:2)

您需要将 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以便将信息传递给它。

因此,您可以在“界面”构建器中从“主”到“细节”控制器创建segue。然后从 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中声明它。