scrollToItemAtIndexPath导致应用崩溃

时间:2014-04-17 07:25:36

标签: ios objective-c

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

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

崩溃:

  

**由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'集合视图的数据源未从-collectionView:cellForItemAtIndexPath返回有效单元格:索引路径{length = 2,path = 0 - 0}'

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

谢谢!

修改

这是我的集合视图方法 - 如果满足条件,我正在加载三种不同类型的单元格:

-(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;

}

3 个答案:

答案 0 :(得分:1)

试试滚动到顶部:

[self.collectionView setContentOffset:CGPointZero animated:YES];

希望这会有所帮助.. :)

答案 1 :(得分:1)

问题在于行

return loaderCell;

位于if语句

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

因此,有时该方法会在返回nil时返回loaderCell

答案 2 :(得分:0)

使用[NSIndexPath indexPathForItem:0 inSection:0]查看是否有效