【问题标题】:iOS 7: insertItemsAtIndexPaths: scrolls the UICollectionViewiOS 7:insertItemsAtIndexPaths:滚动 UICollectionView
【发布时间】:2013-09-27 22:12:38
【问题描述】:

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

这一切都在 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;
    }];
}

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

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

    }

}

【问题讨论】:

  • 我无法重现这个。你可以看到无限滚动的集合视图我mocked up here。每次你触底时,它都会增加 200 个细胞。那么,你能展示你的代码吗?

标签: objective-c ios6 uikit ios7 uicollectionview


【解决方案1】:

没有看到你的代码就很难回答。

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

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:NO

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

【讨论】:

  • 你的意思是我在插入新项目后使用这种方法将滚动位置设置为特定项目?我试过这个并没有解决问题...它首先设置了新的滚动位置,然后整个东西再次滚动到顶部。
猜你喜欢
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多