删除tableviewCell出错了

报错:

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)
}
}
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容