Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7: insertItemsAtIndexPaths: scrolls the UICollectionView

I'm developing an app where I have an infinitely scrolling UICollectionView of images. When the user reaches the bottom of the page, I query for the next page of images, get the new indexPaths, and add them to the UICollectionView with [collectionView insertItemsAtIndexPaths:newIndexPaths].

This all works perfectly in iOS 6. In iOS 7 it works, but it scrolls the user back up to the top of the collectionView each time. I've 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 is a different size, so I'm using RFQuiltLayout for this. I don't think this has anything to do with the scrolling issue, since the source code doesn't seem to involve scrolling.

I load each set of 12 images from our Parse backend with the following code. (If isRefresh is true, I clear out the array of images and load the first page again):

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

I then detect when the user scrolls to the bottom of the page and load the next set with 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];
        }

    }

}
like image 214
Andrew Avatar asked Sep 27 '13 22:09

Andrew


1 Answers

Difficult to answer without seeing your code.

Can you try using scrollToItemAtIndexPath method?

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

You can scroll to any position that you want.

like image 195
user2734323 Avatar answered Oct 17 '22 16:10

user2734323