diff --git a/RMGallery/RMGalleryView.m b/RMGallery/RMGalleryView.m index 0be6330..1bdd4df 100644 --- a/RMGallery/RMGalleryView.m +++ b/RMGallery/RMGalleryView.m @@ -129,13 +129,15 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { RMGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; - + NSInteger imageIndex = indexPath.row; + cell.tag = imageIndex; // mark the cell so we're sure it has not been reused when we fill it with the image + [cell.activityIndicatorView startAnimating]; + __block BOOL sync = YES; - [self.galleryDataSource galleryView:self imageForIndex:indexPath.row completion:^(UIImage *image) { + [self.galleryDataSource galleryView:self imageForIndex:imageIndex completion:^(UIImage *image) { // Check if cell was reused - NSIndexPath *currentIndexPath = [self indexPathForCell:cell]; - if (!sync && [indexPath compare:currentIndexPath] != NSOrderedSame) return; + if (!sync && cell.tag != imageIndex) return; [cell.activityIndicatorView stopAnimating]; cell.image = image; diff --git a/RMGallery/RMGalleryViewController.m b/RMGallery/RMGalleryViewController.m index b1b8a52..6d0e3fb 100644 --- a/RMGallery/RMGalleryViewController.m +++ b/RMGallery/RMGalleryViewController.m @@ -72,9 +72,15 @@ - (void)viewDidLoad [self.view addGestureRecognizer:_tapGestureRecognizer]; } -- (void)viewWillAppear:(BOOL)animated +//- (void)viewWillAppear:(BOOL)animated +//{ +// [super viewWillAppear:animated]; +//} + +- (void)viewDidLayoutSubviews { - [super viewWillAppear:animated]; + [super viewDidLayoutSubviews]; + if (!_initialGalleryIndexSet) { // In case the gallery index was set before the view was loaded