iOS tableView删除行(爬坑记)

有些时间没有做这个操作了:),突然又在项目中遇到这个需求,凭着记忆开始操作!写好代码运行!!!结果……..
砰地一声崩了!错误如下:Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), 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.beginUpdates()
let model:MessageModel = modelArray![indexPath.row]
let index:IndexPath = IndexPath(row: modelArray!.index(of: model)!, section: 0)
modelArray?.remove(at: modelArray!.index(of: model)!)
tableView.deleteRows(at: [index], with: .automatic)
tableView.endUpdates()
丢了加粗的两句,从而导致table在取行数的时候出现错误!!!切记!!!删除添加行的时候操作代码一定要在updatable之间!!!

你可能感兴趣的:(总结)