我在Swift、iOS 8和Xcode 6 Beta 6中的tableView中删除一行时遇到了麻烦。每次尝试删除一行时,都会出现类似以下错误的信息:
``` Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3302.3.1/UITableView.m:1581 2014-08-30 20:31:00.971 Class Directory[13290:3241692] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (25) must be equal to the number of rows contained in that section before the update (25), 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). ``` 我已经阅读了所有关于这个常见问题的答案,并且感觉我已经满足建议的条件。似乎从数据模型中删除了该项--当我重新加载应用程序时,被删除的项已经从表格中消失--但是在相应的sqlite文件中仍存在一些残留物,当然,数学不符合。输出indexPath的println显示正确的Section和Row。 我非常困惑。这应该很简单,但我肯定错过了一些愚蠢的东西,我怀疑是在数据模型删除中。完整项目在Github上。
``` Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3302.3.1/UITableView.m:1581 2014-08-30 20:31:00.971 Class Directory[13290:3241692] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (25) must be equal to the number of rows contained in that section before the update (25), 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). ``` 我已经阅读了所有关于这个常见问题的答案,并且感觉我已经满足建议的条件。似乎从数据模型中删除了该项--当我重新加载应用程序时,被删除的项已经从表格中消失--但是在相应的sqlite文件中仍存在一些残留物,当然,数学不符合。输出indexPath的println显示正确的Section和Row。 我非常困惑。这应该很简单,但我肯定错过了一些愚蠢的东西,我怀疑是在数据模型删除中。完整项目在Github上。
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return fetchedResultController.sections.count
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return fetchedResultController.sections[section].numberOfObjects
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableViewMain.dequeueReusableCellWithIdentifier("CellMain", forIndexPath: indexPath) as UITableViewCell
let personForRow = fetchedResultController.objectAtIndexPath(indexPath) as Person
cell.textLabel.text = personForRow.fullName()
return cell
}
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
println("section and row \(indexPath.section) \(indexPath.row) ")
if (editingStyle == UITableViewCellEditingStyle.Delete) {
let personForRow : NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as Person
context?.deleteObject(personForRow)
context?.save(nil)
tableViewMain.beginUpdates()
tableViewMain.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
tableViewMain.endUpdates()
}