我使用以下代碼從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