一个UITableView删除操作时的异常处理

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

‘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

时间: 2024-08-25 01:34:12

一个UITableView删除操作时的异常处理的相关文章

mysql中进行删除操作时用到not in 导致删除不成功

delete from tb_news where id not in ( select max(id) From tb_news Group By title ) 刚开始用这条语句删除一直不成功 然后百度了一下,说是要建立一张临时表 于是进行了以下操作 先建立一个临时表 CREATE TEMPORARY TABLE tmp_news ( `id` BIGINT(20) ) 然后执行以下语句 delete from tb_news where id not in (SELECT * from (

php文件rmdir删除操作时,出现Permission denied,解决方法

Say, you're working on Windows and continue to get a permission's error without a reason. Then it may be that a different Windows program is working on the folder (see earlier notes also). In the case that you can't find that program, the line <?php 

浅谈红黑树的添加删除操作

红黑树的性质(牢记) 1.每个结点的颜色只能是红色或黑色. 2.根结点必须是黑色的. 3.每个叶子结点都带有两个空的黑色结点(被称为黑哨兵null),如果一个结点n的只有一个左孩子,那么n的右孩子是一个黑哨兵:如果结点n只有一个右孩子,那么n的左孩子是一个黑哨兵. 4.如果一个结点是红的,则它的两个儿子都是黑的.也就是说在一条路径上不能出现相邻的两个红色结点. 5.从任何一个结点到其子孙叶结点的所有路径上包含相同数目的黑结点. 一.红黑树的插入 任何一个即将插入的新结点的初始颜色都为红色.这一点

dojo处理删除操作报错

1.错误描述    java.lang.IllegalArgumentException:attempt to create delete event with null entity. 2.错误原因    dojo.xhrPost({         url:"", content:{     "userId":userId         },         success:function(data)         {         }    });  

解决新建短信时,输入“+86”,然后输入联系人名字“1”,按删除键之后,联系人变为“1”,删除操作为达到预期结果

1.保存一个"+8618621058121"号码的联系人"Jane" 2.保存一个号码是"42266888"的联系人"1" 3.信息-新建信息-在联系人输入框中输入"+86"-输入"1"-点击删除按钮 [测试结果]联系人变为1 [预期结果]联系人应为"Jane" 与问题相关的类为Mms下的ComposeMessageActivity,相关控件为在initRecipie

FileDemo1~3 使用File表示一个文件或目录时的操作

/** *    File的每一个实例用于表示操作系统里 文件系统中的 一个文件或目录 * *  当使用File表示一个文件或目录时,可以: *  1.访问该文件或目录的属性信息,例如:文件名,最后修改时间,文件大小等 *  2.可以操作文件或目录(创建,删除) *  3.可以访问该目录的所有子项,但不能访问文件数据 */ public class FileDemo1 { public static void main(String[] args) { File file = new File(

扩展BindingList,防止增加、删除项时自动更新界面而不出现“跨线程操作界面控件 corss thread operation”异常

在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件.但对于数据绑定bindingList而言,没法响应listchang事件,导致后端的grid等控件不能更新数据.废了好大的劲终于找到一个UIBindingList,实现线程数据的同步! using System; using System.ComponentModel; using System.Threading; using System.Wi

删除数据时弹出一个确认对话框

使用js实现点击按钮删除数据时弹出一个确认是否删除的对话框: <input type="button" class="wz2 btn" value="删除" onClick="sc('{$vo.name}','__URL__/delete?aid={$vo.id}')"/> <script type="text/javascript"> function sc($name,$ulr)

UITableView删除分组最后一个元素

UITableView删除分组最后一个元素,需要把分组同时删除. 1.首先在数据源上删除数据. 2.UI上删除cell: [tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 3.删除数据源上分组的元素. 4.UI上删除分组元素: [tableView deleteSections:[NSIndexSetind