gpt4 book ai didi

ios - 搜索栏 Controller 和 NSFetchedResultsController

转载 作者:行者123 更新时间:2023-11-28 08:51:38 25 4
gpt4 key购买 nike

我有一个带有搜索栏 Controller 和 NSFetchedResultsController 的表:

class CartViewController: UIViewController, NSFetchedResultsControllerDelegate, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

switch type {
case .Insert:
if let _newIndexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([_newIndexPath], withRowAnimation: .Fade)
}
case .Delete:
if let _indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([_indexPath], withRowAnimation: .Fade)
}
case .Update:
if let _indexPath = indexPath {
tableView.reloadRowsAtIndexPaths([_indexPath], withRowAnimation: .Fade)
}
default:
self.tableView.reloadData()
}

self.products = controller.fetchedObjects as! [Product]
}

}

我都喜欢这个tutorial .一切正常。但是如果我在搜索时尝试删除行:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete",handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

let product = (self.searchController.active) ? self.searchResults[indexPath.row]:self.products[indexPath.row]
product.setValue(false, forKey: "inCart")
do {
try self.appDelegate.managedObjectContext.save()
} catch {
fatalError("Failure to save context: \(error)")
}

})
}

我收到错误:

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

当我使用搜索栏 Controller 时如何编辑元素?

最佳答案

您正在更新由获取的结果 Controller 获取的数据集,而不更新 TableView 。

了解您必须为更改准备 TableView ,然后重新加载 TableView ,确保您的 TableView 数据源方法响应数据集中的更改。

您可能还需要确保您的 TableView 委托(delegate)方法响应更改,具体取决于您的实现。

关于ios - 搜索栏 Controller 和 NSFetchedResultsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34049629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com