現象:刪除某行的時候,出現如下異常信息
'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