我有以下代码从Core Data中删除记录:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.Delete) {
let recordToDelete = varUserClients[indexPath.row]
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
managedContext.deleteObject(recordToDelete)
do {
try managedContext.save()
} catch let error as NSError {
NSLog("Could not save \(error), \(error.userInfo)")
}
clientsTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
clientsTableView.reloadData()
}
}
代码成功删除了记录,但它也崩溃了应用程序。其他一切都很好。我在这里做错了什么?
PS:这是我收到的错误消息:
由于未捕获的异常而终止应用 'NSInternalInconsistencyException',原因:'无效更新:无效 第0节中的行数。包含在中的行数 更新后的现有部分(1)必须等于数量 在更新(1)之前,该部分中包含的行加或减 从该部分插入或删除的行数(插入0, 删除1)加上或减去移入或移出的行数 该部分(0移入,0移出)。
PPS:根据@GeneratorOfOne的要求,以下是代码的其余部分:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return varUserClients.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let IndividualCell = clientsTableView.dequeueReusableCellWithIdentifier("ClientCell", forIndexPath: indexPath) as! CUSTOMCLIENTS
let client = varUserClients[indexPath.row]
let varFirstName = client.valueForKey("clientsFirstName") as! String
let varLastName = client.valueForKey("clientsLastName") as! String
let varFullName = String("\(varFirstName) \(varLastName)")
IndividualCell.outletLabel_UserClientFullName.text = varFullName
IndividualCell.outletLabel_UserClientPhoneNumber.text = (client.valueForKey("clientsMobilePhoneNumber") as! String)
IndividualCell.outletLabel_UserClientEmailAddress.text = (client.valueForKey("clientsWorkEmailAddress") as! String)
IndividualCell.outletLabel_UserClientPosition.text = (client.valueForKey("clientsJobTitle") as! String)
IndividualCell.outletLabel_UserClientDistributionList.text = (client.valueForKey("clientsDistributionList") as! String)
let varSelectPriorityPic = client.valueForKey("clientsDistributionList") as! String
switch varSelectPriorityPic {
case constRiskMatrixCodeLevel01:
IndividualCell.outletImage_UserClientDistributionListImage.image = UIImage(named: "clientpriority1")
case constRiskMatrixCodeLevel02:
IndividualCell.outletImage_UserClientDistributionListImage.image = UIImage(named: "clientpriority2")
case constRiskMatrixCodeLevel03:
IndividualCell.outletImage_UserClientDistributionListImage.image = UIImage(named: "clientpriority3")
case constRiskMatrixCodeLevel04:
IndividualCell.outletImage_UserClientDistributionListImage.image = UIImage(named: "clientpriority4")
case constRiskMatrixCodeLevel05:
IndividualCell.outletImage_UserClientDistributionListImage.image = UIImage(named: "clientpriority5")
default:
break
}
return IndividualCell
}
答案 0 :(得分:1)
您还必须从数据源阵列中删除该项目。
我建议订单首先从数据源(模型)和表格视图(视图)中删除项目,然后删除核心数据中的项目。
重要提示:deleteRowsAtIndexPaths
会自动重新排序表格。您不得致电reloadData()
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.Delete) {
let recordToDelete = varUserClients[indexPath.row]
varUserClients.removeAtIndex(indexPath.row)
clientsTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
managedContext.deleteObject(recordToDelete)
do {
try managedContext.save()
} catch let error as NSError {
NSLog("Could not save \(error), \(error.userInfo)")
}
}
}
答案 1 :(得分:0)
您可以使用NSFetchedResultsController
及其委托协议方法。然后您可以轻松管理tableView。如果要在Core Data中的UITableView上删除,更新或插入对象!
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.Delete) {
let recordToDelete = varUserClients[indexPath.row]
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
managedContext.deleteObject(recordToDelete)
do {
try managedContext.save()
} catch let error as NSError {
NSLog("Could not save \(error), \(error.userInfo)")
}
// clientsTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
// This is should be works! Try this out
clientsTableView.reloadData()
}
}