【发布时间】:2016-04-17 10:12:36
【问题描述】:
我有一个 NSFetchedResultsController 显示来自 Core Data 的一些数据,我正在尝试让搜索栏使用它。搜索有效,但 controller(didChangeObject:) 是使用来自完整表的索引路径调用的,而不是过滤的,因此更新要么修改错误的记录,要么因为索引路径超出范围而导致错误。尽管我知道这是错误的根源,但我不确定如何修复它。我的代码的主要部分如下:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("PlayerTableViewCell", forIndexPath: indexPath) as! PlayerTableViewCell
let coreDataIndexPath: NSIndexPath
if searchController.active && searchController.searchBar.text != "" {
coreDataIndexPath = fetchedResultsController.indexPathForObject(filteredPlayers[indexPath.row])!
} else {
coreDataIndexPath = indexPath
}
configureCell(cell, atIndexPath: coreDataIndexPath)
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Fetch Record
let record = fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
// Delete Record
managedObjectContext.deleteObject(record)
}
}
// MARK: Fetched Results Controller Delegate Methods
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch (type) {
case .Insert:
if let indexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
case .Delete:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
case .Update:
if let indexPath = indexPath {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! PlayerTableViewCell
configureCell(cell, atIndexPath: indexPath)
}
case .Move:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if let newIndexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
}
}
}
// MARK: UISearchResultsUpdating
func updateSearchResultsForSearchController(searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
let predicate = NSPredicate(format: "name CONTAINS %@", searchText)
if let fetchedObjects = fetchedResultsController.fetchedObjects as? [Player] {
filteredPlayers = fetchedObjects.filter({return predicate.evaluateWithObject($0)})
}
}
tableView.reloadData()
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.active && searchController.searchBar.text != "" {
return filteredPlayers.count
} else if let sections = fetchedResultsController.sections {
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}
return 0
}
configureCell 只是将 indexPath 记录中的数据加载到单元格中。
我之前曾尝试通过更改fetchedResultsController.fetchRequest.predicate 进行搜索,但尽管我尽了最大努力,但当搜索栏被取消时,表格仍无法恢复正常。该代码如下:
// MARK: UISearchResultsUpdating
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchText = searchController.searchBar.text!
let predicate = NSPredicate(format: "%K CONTAINS[c] %@", argumentArray: ["name", searchText])
fetchedResultsController.fetchRequest.predicate = predicate
do {
try self.fetchedResultsController.performFetch()
} catch {
let fetchError = error as NSError
print("\(fetchError), \(fetchError.userInfo)")
}
tableView.reloadData()
}
// MARK: UISearchBarDelegate
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
fetchedResultsController.fetchRequest.predicate = nil
do {
try self.fetchedResultsController.performFetch()
} catch {
let fetchError = error as NSError
print("\(fetchError), \(fetchError.userInfo)")
}
tableView.reloadData()
}
【问题讨论】:
-
你有没有想过这个问题?我也有同样的问题。
标签: ios swift nsfetchedresultscontroller nsfetchrequest uisearchcontroller