重构为香草UIViewController
而不是让UITableView
在有效和无效UISearchController
之间切换,我重构为一个香草UIViewController
,顶部有UISearchBar
,UITableView
直接在它下面。
我要做的是在fetchedResultsController
之间切换一个由searchBar中的文本设置的谓词和一个抓取所有内容的谓词。
我找到了这个答案,它描述了Objective-C中类似问题的解决方案,并作为我重构Swift项目的基础:
如何使用UISearchDisplayController / UISearchBar过滤NSFetchedResultsController(CoreData) https://stackoverflow.com/a/9512891/4475605
我的问题:如何更新searchBar
文字的fetchedResultsController?
tableView
填充正确,但我无法弄清楚如何为fetchedResultsController
更新searchPredicate
这是我尝试过的。
以下是我宣布NSPredicate
& NSFetchedResultsController
位于班级的顶部:
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)
}
}
感谢您的阅读。我欢迎你的建议。
答案 0 :(得分:15)
这不是那么快 - 伪代码......它肯定不会用任何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()
}