简体   繁体   English

iOS Swift 2 - 删除tableView行时出错

[英]iOS Swift 2 - Error deleting tableView row

I've an error when I try to delete a row from a tableView. 我尝试从tableView中删除一行时出错。 The code goes like the following 代码如下所示

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
 if editingStyle == UITableViewCellEditingStyle.Delete {

     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
     tableView.reloadData()

 }

} }

The error printed from Xcode is the following: 从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). 无效更新:第0节中的行数无效。更新(1)后现有部分中包含的行数必须等于更新前的该部分中包含的行数(1),加上或减去数字从该部分插入或删除的行(插入0,删除1)并加上或减去移入或移出该部分的行数(0移入,0移出)。

I've read here on Stack this error but I can't figure it out. 我在这里读到Stack这个错误,但我无法弄明白。 :( :(

Thanks in advance. 提前致谢。

You deleted the row from the table, but first you need to delete the corresponding datum from your data model. 您从表中删除了该行,但首先需要从数据模型中删除相应的数据。 For example, if your data model is an array, then you might call removeAtIndex on it to remove that row. 例如,如果您的数据模型是一个数组,那么您可以在其上调用removeAtIndex来删除该行。 You must always proceed in this order: adjust the data model, adjust the table view. 您必须始终按此顺序进行操作:调整数据模型,调整表格视图。

(There is no need to reload the data at this time; you can omit that line.) 无需重新加载,此时的数据,则可以省略线。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM