iOS Swift 2 - 删除 tableView 行时出错
iOS Swift 2 - Error deleting tableView row
当我尝试从 tableView 中删除一行时出错。
代码如下
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.reloadData()
}
}
从 Xcode 打印的错误如下:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), 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).
我已经在 Stack 上阅读了此错误,但我无法弄清楚。
:(
提前致谢。
您从 table 中删除了该行,但首先您需要从数据模型中删除相应的数据。例如,如果您的数据模型是一个数组,那么您可以对其调用 removeAtIndex
以删除该行。您必须始终按以下顺序进行:调整数据模型,调整 table 视图。
(此时不需要重新加载数据,可以省略那一行。)
当我尝试从 tableView 中删除一行时出错。 代码如下
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.reloadData()
}
}
从 Xcode 打印的错误如下:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), 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).
我已经在 Stack 上阅读了此错误,但我无法弄清楚。 :(
提前致谢。
您从 table 中删除了该行,但首先您需要从数据模型中删除相应的数据。例如,如果您的数据模型是一个数组,那么您可以对其调用 removeAtIndex
以删除该行。您必须始终按以下顺序进行:调整数据模型,调整 table 视图。
(此时不需要重新加载数据,可以省略那一行。)