在Core Data应用程序中,我们可以滑动删除一行,但我想通过选择删除多行。
self.tableView.allowsMultipleSelectionDuringEditing = true
我已经完成了上述操作,并且我还实现了通常的删除方法。我也试过这样的事情。
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let entry: DiaryEntry = self.fetchedResultsController.objectAtIndexPath(indexPath) as! DiaryEntry
let coreDataStack: CoreDataStack = CoreDataStack.defaultStack
coreDataStack.managedObjectContext.deleteObject(entry)
coreDataStack.saveContext()
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
var multipleSelection = false
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert :
if indexPath != newIndexPath {
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Automatic)
}
case .Delete :
if (multipleSelection == true) {
let selectedRows = self.tableView.indexPathsForSelectedRows
for selectionIndex in selectedRows! {
let indicesOfItemsToDelete = NSMutableIndexSet()
indicesOfItemsToDelete.addIndex(selectionIndex.row)
let entry: DiaryEntry = self.fetchedResultsController.objectAtIndexPath(selectionIndex) as! DiaryEntry
let coreDataStack: CoreDataStack = CoreDataStack.defaultStack
coreDataStack.managedObjectContext.deleteObject(entry)
coreDataStack.saveContext()
}
self.tableView.deleteRowsAtIndexPaths(selectedRows!, withRowAnimation: .Automatic)
} else {
self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
}
case .Update :
self.tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
case .Move :
self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Automatic)
if indexPath != newIndexPath {
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Automatic)
}
}
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Insert :
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Automatic)
case .Delete :
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Automatic)
case .Update :
//let range: NSRange = NSMakeRange(0, (self.fetchedResultsController.sections?.count)!)
self.tableView.reloadSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Automatic)
default:
break
}
}
当我按下编辑方法时,我将tableView的编辑设置为true,选择后,当我按下删除按钮时,以下是代码。
let selectedRows = self.tableView.indexPathsForSelectedRows
multipleSelection = true
for selectionIndex in selectedRows! {
tableView(tableView, commitEditingStyle: .Delete, forRowAtIndexPath: selectionIndex)
}
self.tableView.deleteRowsAtIndexPaths(selectedRows!, withRowAnimation: .Automatic)
self.tableView.reloadRowsAtIndexPaths(selectedRows!, withRowAnimation: .Automatic)
但是,我收到一个错误:CoreData:错误:严重的应用程序错误。在Core Data更改处理期间捕获到异常。这通常是.....观察者的错误。
可以,有人请告诉我解决方案吗?
答案 0 :(得分:2)
请勿通过要求表视图删除(提交编辑类型)来删除。获取要删除的托管对象列表,并自己从上下文中删除它们,然后告诉表格视图它们已经消失(您已经这样做了)。此外,在告诉表格它们消失后,请不要重新加载行。