您的位置:首页 > 其它

关于Tableview 方法insertRowsAtIndexPaths 报错问题

2015-11-20 16:59 495 查看
最近项目中需要使用 Tableview的多级展开效果  折腾了几天终于实现了,通过 insertRowsAtIndexPaths 和 delecRowsAtIndexPaths 方法 但是还没高兴起来另一个坑就有出现在了,在当前页展开Ok 但是点击cell跳转界面 在返回时,再点击折合的cell时就会立马的crash掉,

并抛出Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (8) must be equal to the number of sections contained
in the table view before the update (4), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

这样的问题  然后就开始度娘各种的查找原因,

最后锁定到,是因为tableview在执行insertRowsAtIndexPaths 和 delecRowsAtIndexPaths 动画时,控制器跳出在返回,会导致tableview再次走load方法,使当前numberOfRow显示的数据与之前的数据地址方法变化,这时在进行insert 和delec时 xcode的消息转发机制就会抛出异常

解决方法有几种 :

        第一种: 在你进行插入和删除的时候添加此方法      

            [tableView beginUpdates];

            [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];

            [tableView endUpdates];

         第二种:在viewdidload方法中调用你的填充数据的方法,这样tableview只会进行一次load  防止界面跳转返回后cell的地址放生变化导致的crash

此方法已验证,若有更好的方法请留言!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: