2016-07-28 109 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