我有下面的代码从核心数据删除一条记录:核心数据删除的问题
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
}
显示cellForRowAtIndexPath:和numberOfRowsInSection:方法的实现,然后它可以更容易地评估您的方案。 – Sandeep
@GeneratorOfOne - 完成。 –