前要
最近在弄那个使用UICollectionView的方法的刷新部分数据源的时候出现了crash,
[_dateCollection reloadItemsAtIndexPaths:tempRefreshIndexPath];
崩溃的信息是 attempt to delete item 72 from section 0 which only contains 0 items before the update,一直不知道什么原因,为什么会执行了delete的方法呢,删除cell的操作呢
原因
看了一份别人写的文章,才知道原因意思就是内部会先删除原来的cell再重新生成cell,所以才会出现delete操作,然而我这个错误的是,原来就没有cell。
[self refreshAllSelectIndex:_selectIndex withOldSeletIndex:oldIndex];会调用[_dateCollection reloadItemsAtIndexPaths:tempRefreshIndexPath];这个方法进行局部刷新
我这里先执行的reloadItemsAtIndexPaths:,但是这时的数据源是空的,之后才执行的reloadData方法,当执行reloadItemsAtIndexPaths方法时,数据源是空的执行删除操作的时候就会crash
解决办法
调换一下位置就好了,先刷新数据源,生成了cell之后,再局部刷新数据源,就这样简单的解决,reloadItemsAtIndexPaths只能刷新已经生成cell的数据,没有生成cell的话,会报出crash错误
推荐关于UITableView的文章
iOS调用reloadRowsAtIndexPaths Crash报异常NSInternalInconsistencyException
我也是借鉴于这个文章
在iOS开发中,使用UICollectionView进行局部刷新时遇到一个crash问题,错误信息为attempt to delete item 72 from section 0 which only contains 0 items before the update。原因是先调用了reloadItemsAtIndexPaths:,而此时数据源为空,接着才执行reloadData,导致尝试删除不存在的cell。解决方法是调整顺序,先reloadData生成cell,再进行局部刷新,从而避免crash。参考文章探讨了类似UITableView的crash问题。
1837

被折叠的 条评论
为什么被折叠?



