2016-01-21 170 views
0

我有下面的代码从核心数据删除一条记录:核心数据删除的问题

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

显示cellForRowAtIndexPath:和numberOfRowsInSection:方法的实现,然后它可以更容易地评估您的方案。 – Sandeep

+0

@GeneratorOfOne - 完成。 –

回答

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)") 
     } 
    } 
} 
+0

完美的作品!谢谢,@vadian,我明白你在说什么,这很有道理。非常感激! –

0

您可以使用NSFetchedResultsController及其委托协议方法。然后你可以轻松管理你的tableView。如果你想删除,更新或插入核心数据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() 
    } 
}