重构为原版 UIViewController

我没有让 UITableView 在事件和非事件 UISearchController 之间切换,而是重构为一个普通的 UIViewController,在顶部有一个 UISearchBar,在其正下方有一个 UITableView

我想要做的是在具有由 searchBar 中的文本设置的谓词和抓取所有内容的谓词之间切换 fetchedResultsController

我找到了这个答案,它描述了Objective-C中类似问题的解决方案,并作为重构Swift项目的基础:

如何使用UISearchDisplayController/UISearchBar过滤NSFetchedResultsController(CoreData)
https://stackoverflow.com/a/9512891/4475605

我的问题:如何为 searchBar 文本更新我的 fetchedResultsController?
tableView 正确填充,但我不知道如何为我的 fetchedResultsController 更新 searchPredicate 这是我尝试过的。

下面是我在类的顶部声明我的 NSPredicateNSFetchedResultsController 的方式:

var searchPredicate = NSPredicate()

// Create fetchedResultsController to store search results
lazy var fetchedResultsController: NSFetchedResultsController = {
    let fetchRequest = NSFetchRequest(entityName: "Song")
    let sortDescriptor = NSSortDescriptor(key: "songDescription", ascending: true);
    // ** THESE TWO LINES CAUSE A CRASH, BUT NO COMPILER ERRORS **
    // let predicate = self.searchPredicate
    // fetchRequest.predicate = predicate
    fetchRequest.sortDescriptors = [sortDescriptor]
    fetchRequest.fetchBatchSize = 20

    let frc = NSFetchedResultsController (fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: "songDescription", cacheName: nil)

    frc.delegate = self

    return frc
}()

这是我的 UISearchBarDelegate 方法:
// MARK: - UISearchBarDelegate methods
// called when text changes (including clear)
internal func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    print("searchText = \(searchBar.text)")
    searchPredicate = NSPredicate(format: "(songDescription contains [cd] %@) || (songStar contains[cd] %@)", searchBar.text!, searchBar.text!)
    // TODO: figure out how to update fetchedResultsController w/ predicate
}

// called when cancel button pressed
internal func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
    searchBar.text = ""
    // TODO: flip back to normal fetchResultsController
}

这是我的 NSFetchedResultsControllerDelegate 方法
internal func controllerWillChangeContent(controller: NSFetchedResultsController) {
    print("controllerWillChangeContent")
    tableView.beginUpdates()
}

internal func controllerDidChangeContent(controller: NSFetchedResultsController) {
    print("controllerDidChangeContent")
    tableView.reloadData()
    tableView.endUpdates()
}

internal func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    switch type {
    case .Insert:
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Automatic)
    case .Delete:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
    case .Update:
        tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
    case .Move:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
        tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
    }
}

感谢您的阅读。我欢迎你的建议。

最佳答案

这不是那么 swift 的伪代码……它肯定不会用任何 swift 编译器编译。

您的 FRC 没有使用缓存,因此没有要删除的缓存,我们可以将谓词分配给现有的 FRC 获取请求。

您可能不想对搜索栏中的每个字符更改都执行全新的提取,或者您可能只想对第一个字符进行搜索,并为后续字符使用子集。

// called when text changes (including clear)
internal func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    var predicate:NSPredicate = nil
    if searchBar.text.length != 0 {
        predicate = NSPredicate(format: "(songDescription contains [cd] %@) || (songStar contains[cd] %@)", searchBar.text!, searchBar.text!)
    }
    fetchedResultsController.fetchRequest.predicate = predicate
    fetchedResultsController.performFetch()
    tableView.reloadData()
}

// called when cancel button pressed
internal func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
    searchBar.text = ""
    fetchedResultsController.fetchRequest.predicate = nil
    fetchedResultsController.performFetch()
    tableView.reloadData()
}

关于swift - 为 UISearchBar 设置的谓词更新 fetchedResultsController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33404482/

10-15 15:32