IOS 7: insertItemsAtIndexPaths: scrolls UICollectionView

I am developing an application where I have an infinitely scrollable UICollectionView of images. When the user reaches the bottom of the page, I request the next page of images, get the new indexPaths and add them to the UICollectionView using [collectionView insertItemsAtIndexPaths:newIndexPaths] .

All this works fine in iOS 6. In iOS 7, it works, but each time it scrolls the user to the top of the collection. I tried using reloadData instead, and the same thing happens.

Any idea how I can prevent this scrolling?

UPDATE: Including my code

I have a UICollectionView that displays thumbnails of images in a mosaic pattern (each image has a different size, so I use RFQuiltLayout for this. I don't think this has anything to do with the scroll problem, as the source code doesn't seem to include scrolling .

I download each set of 12 images from our Parse backend using the following code. (If isRefresh true, I clear the image array and reload the first page):

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

Then I detect when the user scrolls to the bottom of the page and loads the following set using this code:

 - (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]; } } } 
+6
source share
1 answer

Difficult to answer without seeing your code.

Can you try using the scrollToItemAtIndexPath method?

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

You can scroll any position you want.

+1
source

Source: https://habr.com/ru/post/954818/


All Articles