ios UICollectionView无效索引路径崩溃

q35jwt9p  于 2023-02-17  发布在  iOS
关注(0)|答案(3)|浏览(202)

UICollectionView尝试滚动到无效索引路径{长度= 2,路径= 0 - 0}时发生崩溃。
这是否意味着我的集合视图大小为2,但我试图滚动到第0节索引0?在这种情况下,这不是一个有效的滚动?
我们已经去掉了使用NSFTechedResultsController的不必要的复杂性,它可能导致崩溃,但我仍然很好奇错误消息是如何读取的,它意味着什么?
这就是密码

void fetch() {

   // get data from core data using a FetchRequest
   do a perform fetch to get my item

   // loop through to see which index the item I want is at
   loop through result {
      if (my id matches core data id) {
          self.selectedIndex = x;
       }
    }

    // get more from core data using a Fetch Request
    create another FetchedResultsController
    do another fetch to get all other items

    [self.myCV reloadData];

    // find the index path for the item I want to scroll to
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectedIndex inSection:0];

    [self.myCV scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally | UICollectionViewScrollPositionCenteredVertically animated:NO];
}

// UICollectionView delegate methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    id<NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [secInfo numberOfObjects];
}
bqf10yzr

bqf10yzr1#

我们也收到了很多相同的崩溃反馈,原因是在reloadData后立即使用scrollToItemAtIndexPath方法,我们使用此代码成功修复。

[self.myCV reloadData];
[self.myCV.collectionViewLayout invalidateLayout];
if (self.myCV.numberOfSections != 0) {
     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectedIndex inSection:0];
    [self.myCV scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally | UICollectionViewScrollPositionCenteredVertically animated:NO];
}
nlejzf6q

nlejzf6q2#

我被困在同一个问题,但我解决了它的取消评论滚动功能只是。

lfapxunr

lfapxunr3#

这个解决方案帮助我模拟的情况:

static var animatingDifferences: Bool {
        if #available(iOS 15.0, *) {
            return true
        } else {
            return false
        }
    }
... // in snapshot applying method:
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
...
// false animations solve crush for iOS 14
apply(snapshot, animatingDifferences: Self.animatingDifferences)

相关问题