【发布时间】:2016-01-29 00:27:46
【问题描述】:
重构为原版UIViewController
我没有让UITableView 在活动和非活动UISearchController 之间切换,而是重构为一个香草UIViewController,顶部是UISearchBar,正下方是UITableView。
我想要做的是在fetchedResultsController 之间切换具有由搜索栏中的文本设置的谓词和抓取所有内容的谓词。
我找到了这个答案,它描述了 Objective-C 中类似问题的解决方案,并作为我重构 Swift 项目的基础:
如何使用 UISearchDisplayController/UISearchBar 过滤 NSFetchedResultsController (CoreData) https://stackoverflow.com/a/9512891/4475605
我的问题:如何为 searchBar 文本更新 fetchedResultsController?
tableView 填充正确,但我不知道如何为我的searchPredicate 更新fetchedResultsController 这是我尝试过的。
这是我在班级顶部声明我的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)
}
}
感谢您的阅读。我欢迎您的建议。
【问题讨论】:
标签: swift uitableview core-data nspredicate nsfetchedresultscontroller