可能刚开始对这个方法的调用也不是很熟,用的时候出现了这么一个错误
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的数量