关于Tableview 方法insertRowsAtIndexPaths 报错问题

最近项目中需要使用 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

此方法已验证,若有更好的方法请留言!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值