我使用以下代码从UITableView中删除一行。当我添加警报代码删除的行会留在的tableView从UITableview中删除行
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let personToDelete = self.fetchResultController.objectAtIndexPath(indexPath) as! Person
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
managedObjectContext.deleteObject(personToDelete)
do{
try managedObjectContext.save()
} catch {
print(error)
}
}
}
}
,一切工作正常 。
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let personToDelete = self.fetchResultController.objectAtIndexPath(indexPath) as! Person
let confirmDelete = UIAlertController(title: "Remove Person", message: "Are you sure to delete \"\(personToDelete.name!)\" and all of its data.", preferredStyle: .ActionSheet)
presentViewController(confirmDelete, animated: true, completion: nil)
let deleteAction = UIAlertAction(title: "Delete", style: .Destructive, handler: { (action : UIAlertAction) in
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
managedObjectContext.deleteObject(subjectToDelete)
do {
try managedObjectContext.save()
} catch {
print(error)
}
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action : UIAlertAction) in
})
confirmDelete.addAction(deleteAction)
confirmDelete.addAction(cancelAction)
presentViewController(confirmDelete, animated: true, completion: nil)
}
}
我试着在beginUpdates和endUpdates上使用一些断点进行调试。 但它似乎当我使用警报,他们不会被调用。
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
print("update ended")
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
print("update Started")
}
没有打印行tableView没有任何改变,除非关闭和打开视图。 当我添加打印线的错误出现
终止应用程序由于未捕获的异常“NSInvalidArgumentException”,理由是:“应用程序试图模态呈现主动控制器
是什么问题?
你是否使用'NSFetchedResultsController'的委托方法,如果是,是所有的方法? – vadian
你有哪些方法可以帮助你。当代码没有提醒时,一切都很好,删除的行将消失,而褪色。当使用警报时,代码根本不会调用controllerDidChangeContent和controllerWillChangeContent和controller方法。 – Milligator