我正在开发一个应用程序,我有一个无限滚动的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];
}
}
}
答案 0 :(得分:1)
如果没有看到您的代码,很难回答。
您可以尝试使用scrollToItemAtIndexPath
方法吗?
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:NO
您可以滚动到所需的任何位置。