2016-07-28 104 views
0

我使用以下代碼從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」,理由是:「應用程序試圖模態呈現主動控制器

是什麼問題?

+0

你是否使用'NSFetchedResultsController'的委託方法,如果是,是所有的方法? – vadian

+0

你有哪些方法可以幫助你。當代碼沒有提醒時,一切都很好,刪除的行將消失,而褪色。當使用警報時,代碼根本不會調用controllerDidChangeContent和controllerWillChangeContent和controller方法。 – Milligator

回答

0

您需要重新加載的tableView一旦你從managedObjectContext

managedObjectContext.deleteObject(subjectToDelete) 
yourTableView.reloadData() 

而對於動漫與刪除操作刪除對象

yourTableView.beginUpdates() 
managedObjectContext.deleteObject(subjectToDelete) 
yourTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
yourTableView.endUpdates() 
+0

當我使用沒有提示的代碼時,一切正常。當我使用警報代碼時,問題就開始了。 – Milligator

0

的tableview將不顯示更新,直到你不更新它的ui線程。 用於調用dispatch_async()來調用主隊列。

隨時隨地可以刪除(這裏我想從你的核心數據實體)剛剛之後寫這 - dispatch_async(dispatch_get_main_queue){ self.tableView.reloadData() }

附:對不起,答案寫得不好。我正在使用我的手機。

0

您正試圖呈現confirmDelete控制器兩次,您在創建後立即展示它,然後在添加動作之後再次展示它。這就是爲什麼你得到錯誤Application tried to present modally an active controller

+0

我不明白你的意思是兩次。你會介意更多解釋嗎? – Milligator

+0

這行代碼:'presentViewController(confirmDelete,animated:true,completion:nil)'在同一個方法中被調用了兩次,你正試圖展示一個已經出現在你寫的 – juanjo

+0

的控制器。我做錯了。 – Milligator