现象:删除某行的时候,出现如下异常信息

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), 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.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)


实际上是下面的num与删除后的行数不一致。

比如删除后实际行数为5,而下面方法的num一般是某个数组的count,当数组的元素忘记remove的时候,则count可能还是6,这就导致了错误的发生。


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    return num  //错误原因就是这里的num和删除后rows不一致

}


解决方案:

   执行完core data或者数据库的删除之后

   对应ViewController中给table提供存储数据的数组执行remove元素操作

   再执行deleteRowsAtIndexPaths