UISearchBar使用didSelectRowAtIndexPath在CoreData中进行segue

时间:2015-03-01 15:12:47

标签: ios core-data uisearchbar segue didselectrowatindexpath

我将Core Data项目设置为 Master-Detail VC 。我已经为它添加了UISearchBar,它可以正常运行。当我点击单元格时,我尴尬地承认了一段时间(这是不好的)让它正确显示细节。

我一直在尝试使用prepareForSegue,但我的导师建议我使用didSelectRowAtIndexPath代替DetailVC

这是我尝试过的事情:

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
    } else {
        selectedNote = self.searchController.filteredObjects[indexPath.row]            
    }

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

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

    navigationController?.pushViewController(detailViewController, animated: true)
}

使用上面的代码,编译器会说'UISearchController' does not have a member named 'filteredObjects'

我感到困惑,因为我已经在MasterViewController课程的顶部设置了这个。

以下是设置信息:

class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

    var detailViewController: DetailViewController? = nil
    var addNoteViewController:AddNoteViewController? = nil
    var managedObjectContext: NSManagedObjectContext? = nil

    // Added variable for UISearchController
    var searchController: UISearchController!
    var searchPredicate: NSPredicate?
    var filteredObjects : [Note]? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.leftBarButtonItem = self.editButtonItem()

        if let split = self.splitViewController {
            let controllers = split.viewControllers
            let context = self.fetchedResultsController.managedObjectContext
            let entity = self.fetchedResultsController.fetchRequest.entity!
            self.detailViewController = controllers[controllers.count-1].topViewController as? DetailViewController
        }

        // UISearchController setup
        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.searchResultsUpdater = self
        searchController.delegate = self
        searchController.searchBar.sizeToFit()
        self.tableView.tableHeaderView = searchController?.searchBar
        self.tableView.delegate = self
        self.definesPresentationContext = false
    }

以下是UISearchBar方法:

// MARK: - UISearchResultsUpdating Delegate Method
// Called when the search bar's text or scope has changed or when the search bar becomes first responder.
func updateSearchResultsForSearchController(searchController: UISearchController) {
    let searchText = self.searchController?.searchBar.text // steve put breakpoint
    println(searchController.searchBar.text)
    if let searchText = searchText {

        // This sets up the seachPredicate and filteredObjects array
        searchPredicate = NSPredicate(format: "noteBody contains[c] %@ OR noteTitle contains[c] %@", searchText, searchText)
        filteredObjects = self.fetchedResultsController.fetchedObjects?.filter() {
            return self.searchPredicate!.evaluateWithObject($0)
            } as! [Note]?

        self.tableView.reloadData()
        println(searchPredicate)
    }
}

// MARK: - UISearchBar Delegate methods

func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    updateSearchResultsForSearchController(self.searchController)
}

// This resets searchPredicate & filteredObjects when the SearchController is dismissed
func didDismissSearchController(searchController: UISearchController) {
    println("didDismissSearchController")
    self.searchPredicate = nil
    self.filteredObjects = nil
    self.tableView.reloadData()
}

1 个答案:

答案 0 :(得分:1)

我知道当你盯着你的代码这么久以至于你错过了明显的感觉:

selectedNote = self.searchController.filteredObjects[indexPath.row] 

应该是

if let filteredObjects = filteredObjects {
    selectedNote = filteredObjects[indexPath.row]
} 

正如您filteredObjects声明为MasterViewController

的实例变量