我正在開發一個應用程序,其中有無限滾動的圖像UICollectionView。當用戶到達頁面的底部時,我查詢圖像的下一頁,獲取新的indexPaths,並將它們添加到帶有[collectionView insertItemsAtIndexPaths:newIndexPaths]
的UICollectionView。iOS 7:insertItemsAtIndexPaths:滾動UICollectionView
這完全適用於iOS 6.在iOS 7中它可以工作,但它每次都會將用戶滾動回到collectionView的頂部。我試過用reloadData
來代替,同樣的事情發生。
任何想法如何我可以防止這種滾動?
UPDATE:包括我的代碼
我有一個顯示在馬賽克圖案圖像的縮略圖(每個圖像的大小不同,所以我用RFQuiltLayout此一UICollectionView我不認爲。這與滾動問題有關,因爲源代碼似乎並不涉及滾動。
我從我們的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];
}
}
}
我無法重現這一點。你可以看到我[在這裏嘲笑]的無限滾動集合視圖(https://dl.dropboxusercontent.com/u/2183704/Infinite%20Scroll%20Collection%20View.zip)。每次你到達底部時,它會增加200個細胞。那麼,你能顯示你的代碼嗎? –