报错:
reason: Invalid update: invalid number of sections.
The number of sections contained in the table view
after the update (1) must be equal to the number of
sections contained in the table view before the update
(2), plus or minus the number of sections inserted or
deleted (0 inserted, 0 deleted).
有两种出错会报这个错误:
- 没有删除数据源的数据就进行删除操作
- 当section里只有一个cell时直接进行 ```
self.tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UITableViewRowAnimation.Automatic)
解决:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle,
forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete{
let rowCount = userList.count//先获取数据的个数,即section里有几个cell
let deletedUser = userList.removeAtIndex(indexPath.row)
//从数据库中删除
UserInfoManager.deleteUserObject(deletedUser)
//删除对应的cell ,并设置一个动画
if rowCount == 1{//如果section里只有一个cell就执行这句
self.tableView.deleteSections(NSIndexSet(index: indexPath.section),
withRowAnimation: .Fade)
}else{
self.tableView.deleteRowsAtIndexPaths([indexPath],
withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
}