0
我試圖從核心數據刪除實體,但我得到以下錯誤:試圖從核心數據中刪除一個實體,我得到...?
The number of rows contained in an existing section after the update (10) must be equal to the number of rows contained in that section before the update (10),
plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
這裏是我的tableview功能
// MARK: delete
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let deletedRow:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
if editingStyle == UITableViewCellEditingStyle.Delete {
// remove the deleted item from the model
moContext.deleteObject(scannedVisitors[indexPath.row] as NSManagedObject)
scannedVisitors.removeAtIndex(indexPath.row)
do {
try moContext.save()
print("deleted and saved")
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}
// remove the deleted item from the `UITableView`
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
deletedRow.accessoryType = UITableViewCellAccessoryType.None
print("Deleted + accessory")
}
}
我有fetchRequest在viewDidAppear而不是viewDidLoad中
override func viewDidAppear(animated: Bool) {
do {
let request = NSFetchRequest(entityName: "ScannedVisitor")
scannedVisitors = try moContext.executeFetchRequest(request) as! [ScannedVisitor]
self.tableView.reloadData()
} catch let error as NSError {
print(error)
}
}
當我重新啓動應用程序中的實體已被刪除,但刪除操作過程中出現了運行時錯誤。 我該如何解決這個問題?
[編輯]左看右看我看到這個部分是「解析」 3次
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("Rows \(scannedVisitors.count)")
return scannedVisitors.count
}
,因爲它打印
Rows 0
Rows 0
Rows 6