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?

你可能感兴趣的:(reloadSections之*** Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:])