UITableView删除-Swift IOS
我有一个类似的问题- 我试图从表视图中删除一行,但出现以下错误:UITableView删除-Swift IOS,ios,uitableview,swift,Ios,Uitableview,Swift,我有一个类似的问题- 我试图从表视图中删除一行,但出现以下错误: 由于未捕获异常nsinternalinconsistenceexception”原因而终止应用程序-尝试从节0中删除行0,该节在更新之前仅包含0行 我的代码如下: override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPa
由于未捕获异常nsinternalinconsistenceexception”原因而终止应用程序-尝试从节0中删除行0,该节在更新之前仅包含0行
我的代码如下:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let context: NSManagedObjectContext = self.fetchedResultsController.managedObjectContext
NSLog("numberOfRowsInSection %d", self.tableView.numberOfRowsInSection(o))
if editingStyle == .Delete
{
let alertController = UIAlertController(title: "Delete menu item", message: "Are you sure you want to delete \((self.fetchedResultsController.fetchedObjects as [Menu])[indexPath.row].menuText)?", preferredStyle: UIAlertControllerStyle.Alert)
let defaultAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {
(action: UIAlertAction!) -> Void in
context.deleteObject(self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject)
var menus = self.fetchedResultsController.fetchedObjects as [Menu]
menus.removeAtIndex(indexPath.row)
context.save(nil)
NSLog("numberOfRowsInSection %d", self.tableView.numberOfRowsInSection(o))
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
tableView.reloadData()
});
alertController.addAction(defaultAction)
presentViewController(alertController, animated: true, completion: nil)
}
}
有人能给我一些帮助吗?提前谢谢!尝试Martin在这个问题上的答案。我希望这个帮助你不会删除它两次吗?你调用
上下文.deleteObject
,因此对象被删除。然后你将菜单指定为获取的对象,并尝试使用菜单再次删除它。removeAtIndex()
。整个菜单似乎没有必要。如果有任何问题,您只需重新蚀刻,并在删除tableview行之前执行。是的,就是这样。非常感谢。似乎数据是直接从上下文检索的,不再需要调用DeleteRowstIndExpaths+removeAtIndex这两个方法