iOS 7:insertItemsAtIndexPaths:滚动UICollectionView

And*_*rew 6 objective-c uikit ios6 uicollectionview ios7

我正在开发一个应用程序,我有一个无限滚动的UICollectionView图像.当用户到达页面底部时,我查询下一页图像,获取新的indexPaths,并将它们添加到UICollectionView中[collectionView insertItemsAtIndexPaths:newIndexPaths].

这一切都在iOS 6中完美运行.在iOS 7中它可以工作,但每次都会将用户滚动回到collectionView的顶部.我尝试过使用reloadData而且会发生同样的事情.

知道如何阻止这种滚动吗?

更新:包括我的代码

我有一个UICollectionView,以马赛克图案显示图像的缩略图(每个图像的大小不同,所以我正在使用RFQuiltLayout.我不认为这与滚动问题有任何关系,因为源代码没有似乎涉及滚动.

我使用以下代码从Parse后端加载每组12个图像.(如果isRefresh是,我清除了图像数组并再次加载第一页):

- (void)loadImagesStartingAtNum:(int)num withRefresh:(BOOL)isRefresh {
    NSLog(@"Starting query...");

    [PFCloud callFunctionInBackground:@"homeFeed" withParameters:@{@"startNum": [NSNumber numberWithInt:num]} block:^(id objects, NSError *error) {

        if(!error) {

            //NSLog(@"Got back home feed objects: %@", objects);

            NSMutableArray *newIndexPaths = [[NSMutableArray alloc] init];

            if (isRefresh) {
                [imagesArray removeAllObjects];
                [imageURLsArray removeAllObjects];

                page = 0;

                for (PFObject *object in objects) {
                    [imagesArray addObject:object];
                    [imageURLsArray addObject:[XSUtilities urlForImageObject:object]];

                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([imagesArray count] - 1) inSection:0];
                    NSLog(@"New row: %d", indexPath.row);
                    [newIndexPaths addObject:indexPath];
                }

                if ([newIndexPaths count] > 0) {
                    [feed reloadData];
                    [refreshControl endRefreshing];

                    page += 1;
                }
            } else {
                for (PFObject *object in objects) {
                    [imagesArray addObject:object];
                    [imageURLsArray addObject:[XSUtilities urlForImageObject:object]];

                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([imagesArray count] - 1) inSection:0];
                    NSLog(@"New row: %d", indexPath.row);
                    [newIndexPaths addObject:indexPath];
                }

                if ([newIndexPaths count] > 0) {
                    [UIView animateWithDuration:0.25 animations:^{loadingLabel.alpha = 0.0;}];
                    [feed insertItemsAtIndexPaths:newIndexPaths];
                    //[feed scrollToItemAtIndexPath:newIndexPaths[0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];

                    NSLog(@"imagesArray count: %d", [imagesArray count]);

                    //[feed reloadData];

                    page += 1;
                } else {
                    NSLog(@"Got back no objects");
                }
            }

        }
        isLoadingNextPage = NO;
    }];
}
Run Code Online (Sandbox Code Playgroud)

然后我检测用户何时滚动到页面底部并使用以下代码加载下一组:

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {

    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;

    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;

    float reload_distance = 480;
    if(y > h - reload_distance) {
        NSLog(@"Hit the load point");

        if (!isLoadingNextPage) {
            NSLog(@"Loading page %d", page);
            isLoadingNextPage = YES;
            [self loadImagesStartingAtNum:(page * 12) withRefresh:NO];
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

use*_*323 1

如果没有看到您的代码,很难回答。

你可以尝试使用scrollToItemAtIndexPath方法吗?

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:NO
Run Code Online (Sandbox Code Playgroud)

您可以滚动到任何您想要的位置。

  • 您的意思是我在插入新项目后使用此方法将滚动位置设置为特定项目吗?我尝试了这个,但它没有解决问题......它首先设置新的滚动位置,然后整个内容再次滚动到顶部。 (2认同)