我正在开发一个应用程序,其中有一个无限滚动的 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;
}];
}
然后我检测用户何时滚动到页面底部并使用以下代码加载下一组:
- (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];
}
}
}