reloadSections之*** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:]

查了很多关于这个报错的解决方法,一般都是调用如下方法:

- (void)insertItemsAtIndexPaths:(NSArray *)indexPaths;

- (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths;

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths;

- (void)moveItemAtIndexPath:(NSIndexPath*)indexPath toIndexPath:(NSIndexPath*)newIndexPath;

很多的解决方式是:

[self.photoCollectionView performBatchUpdates:^{

//code....

} completion:nil];

而调用

- (void)reloadSections:(NSIndexSet*)sections;

刷新某组数据时提示:

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 (76) must be equal to the number of items contained in that section before the update (70), plus or minus the number of items inserted or deleted from that section (5 inserted, 2 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

解决方法

先要确认要刷新的section有item,或该section已经加载出item

1、调用  - (NSInteger)numberOfItemsInSection:(NSInteger)section;方法

eg:

if([self.photoCollectionView numberOfItemsInSection:sectionIndex]) {

NSIndexSet*indexSet = [NSIndexSetindexSetWithIndex:sectionIndex];

[self.photoCollectionView reloadSections:indexSet];

}

2、调用 - (nullableUICollectionViewCell*)cellForItemAtIndexPath:(NSIndexPath*)indexPath;方法

eg:

UICollectionViewCell*cell = [self.photoCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:sectionIndex]];

if(cell !=nil) {

NSIndexSet*indexSet = [NSIndexSet indexSetWithIndex:sectionIndex];

[self.photoCollectionView reloadSections:indexSet];

}

这样问题就解决啦!

参考:

Assertion error when reloading sections of UICollectionView in performBatchUpdates:

How to solve this CollectionView crash?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容