私はNSFetchedResultsController
コアデータからいくつかのデータを表示しており、私はそれで動作する検索バーを取得しようとしています。検索は機能しますが、フィルタリングされたテーブルではなくフルテーブルからインデックスパスを使用してcontroller(didChangeObject:)
が呼び出されるため、インデックスパスが範囲外であるため、更新によって間違ったレコードまたはエラーが修正されます。これがエラーの原因であることは分かっていますが、修正方法がわかりません。次のように私のコードの顕著な部分がある:NSFetchedResultsController UpdateをUISearchControllerで動作させるにはどうしたらいいですか?
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()
}
あなたがこれを理解しましたか?私は同じ問題を抱えている。 – NRitH