gpt4 book ai didi

ios - scrollToItemAtIndexPath 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 10:43:56 29 4
gpt4 key购买 nike

我正在尝试通过代码将我的 UICollectionView 滚动到顶部。我正在调用这个方法:

        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];

崩溃:

** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the collection view's data source did not return a valid cell from -collectionView:cellForItemAtIndexPath: for index path {length = 2, path = 0 - 0}'

不太确定如何解决这个问题?

谢谢!

编辑

这是我的 Collection View 方法 - 如果满足条件,我将加载三种不同类型的单元格:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{ static NSString *tableCellIdentifier = @"TableItemCell";
static NSString *gridCellIdentifier = @"GridItemCell";
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

if (indexPath.item < [self.tradeSearchArray count]){

if (self.gridLayoutActive == NO){

self.switchToTableLayout = NO;
BBItemTableViewCell *tableItemCell = [collectionView dequeueReusableCellWithReuseIdentifier:tableCellIdentifier forIndexPath:indexPath];

if ([self.tradeSearchArray count] > 0){

self.toolBarButtomItem.title = [NSString stringWithFormat:@"%d Results", self.searchResult.searchResults];

self.switchToTableLayout = NO;

tableItemCell.gridView = NO;
tableItemCell.backgroundColor = [UIColor whiteColor];
tableItemCell.item = self.tradeSearchArray[indexPath.row];

}
return tableItemCell;
}else

{

BBItemTableViewCell *gridItemCell= [collectionView dequeueReusableCellWithReuseIdentifier:gridCellIdentifier forIndexPath:indexPath];

if ([self.tradeSearchArray count] > 0){

self.toolBarButtomItem.title = [NSString stringWithFormat:@"%d Results", self.searchResult.searchResults];
self.switchToTableLayout = YES;

gridItemCell.gridView = YES;
gridItemCell.backgroundColor = [UIColor whiteColor];
gridItemCell.item = self.tradeSearchArray[indexPath.row];

}

return gridItemCell;
}
}else
{

BBLoaderCell *loaderCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LoaderCell" forIndexPath:indexPath];
loaderCell.backgroundColor = [UIColor clearColor];


if (self.maximumOffset - self.currentOffset <= 1000.0) {
[self loadMoreSearchResults];

if (self.loadingMore){
dispatch_async(dispatch_get_main_queue(), ^{
activityIndicator.hidden = NO;
[loaderCell.spinner startAnimating];
});
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
activityIndicator.hidden = YES;
[loaderCell.spinner stopAnimating];
});
}

return loaderCell;
}
}

return nil;

}

最佳答案

问题是线路

return loaderCell;

if语句中

if (self.maximumOffset - self.currentOffset <= 1000.0)

因此,有时该方法本应返回 loaderCell,但会返回 nil

关于ios - scrollToItemAtIndexPath 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23127032/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com