ios-collectionView?.deleteItems(at: [indexPath])造成的错误

可能刚开始对这个方法的调用也不是很熟,用的时候出现了这么一个错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (2) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

这个错误的原因,我也调试了很久,看了几篇文章,我发现问题所在就在于在调用

collectionView?.deleteItems(at: [indexPath])会去调用

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {  } 这个方法,

也就是说如果我们删除了一个cell,这个时候在调用numberOfItem 这个方法的时候cell还是没有删除的,

如果比如说我们collectionView中的cell只剩下了1个,而我们这个数据源方法确说返回两个,就会报这个错误。

我断点调试,删除item是不会去调用override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { }这个方法的。所以这一点我们需要注意的。

而reloadData方法只是单纯的去刷新数据,不会有动画的产生,以及不会检查item的数量

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值