4

I am trying to implement an "endless scroll" with UICollectionView.

I do this by buffering my data array like in this tutorial

and then by implementing the didEndDisplayingCell of UICollectionViewDelegate in the following manner:

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
if (self.galleryArray.count > 0) {

    NSIndexPath *newIndexPath = indexPath;

    if (self.specialHeaderView.bannerView.scrollDirection == left) {
        newIndexPath = [NSIndexPath indexPathForItem:indexPath.row - 1 inSection:indexPath.section];
    } else if (self.specialHeaderView.bannerView.scrollDirection == right) {
        newIndexPath = [NSIndexPath indexPathForItem:indexPath.row + 1 inSection:indexPath.section];
    }

    if (newIndexPath.row == (self.galleryArray.count - 1)) {
        // user is scrolling to the right from the last item to the 'fake' item 1.
        // reposition offset to show the 'real' item 1 at the left-hand end of the collection view

        newIndexPath = [NSIndexPath indexPathForItem:1 inSection:indexPath.section];

        [self.bannerCollectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        return;

    }

    //        if (scrollView.contentOffset.x == self.collectionView.frame.size.width)  {
    if (newIndexPath.row == 0) {
        // user is scrolling to the left from the first item to the fake 'item N'.
        // reposition offset to show the 'real' item N at the right end end of the collection view

        newIndexPath = [NSIndexPath indexPathForItem:([self.galleryArray count] -2) inSection:indexPath.section];

        [self.bannerCollectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

    }


}

}

Problem is, whenever the didEndDisplayingCell method gets called and collection view requests a cell via it's delegate CellForItemAtIndexPath method, the cell comes back hidden.

Here is my CellForItemAtIndexPath implementation:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SpecialBannerCell *specialBannerCell = (SpecialBannerCell *)[collectionView dequeueReusableCellWithReuseIdentifier:GalleryCellIdentifier forIndexPath:indexPath];
if (specialBannerCell.hidden) {

}
Benefit *benefit = [self.galleryArray objectAtIndex:indexPath.row];
[specialBannerCell.imageBanner setImageWithURL:[NSURL URLWithString:benefit.imageIphoneUrl] placeholderImage:[UIImage imageNamed:@"photo_loader"]];
return specialBannerCell;

}

What am I doing wrong here?

Lena Bru
  • 13,521
  • 11
  • 61
  • 126
Michal Shatz
  • 1,416
  • 2
  • 20
  • 31
  • Is your cell hidden in your interface builder or your xib file ? – Lena Bru Jun 23 '14 at 20:21
  • @LenaBru Not at all. As i mentioned it does appear most of the time. Anyway, I solved the problem - solution is posted below. – Michal Shatz Jun 24 '14 at 07:23
  • 1
    I am also facing similar issue in cellforrowat index path, i have collection view banner in a Pageview controller, at first time I swipe through the pages, everything is fine, but when I swipe back the cell gets hidden property as YES. The same code works fine in iOS8, it stopped working in iOS9 – anoop4real Aug 11 '16 at 11:38

1 Answers1

0

So I am not sure why but when I used the UIScrollViewDelegate methods instead of the didEndDisplayingCell method cell is not hidden anymore and everything works perfect.

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (self.galleryArray.count > 0) {

    NSIndexPath *indexPath = self.currentIndexPath;

    NSIndexPath *newIndexPath = indexPath;

    if (newIndexPath.row == (self.galleryArray.count - 1)) {
        // user is scrolling to the right from the last item to the 'fake' item 1.
        // reposition offset to show the 'real' item 1 at the left-hand end of the collection view

        newIndexPath = [NSIndexPath indexPathForItem:1 inSection:indexPath.section];

        self.currentIndexPath = newIndexPath;

        [self.bannerCollectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        return;

    }

    if (newIndexPath.row == 0) {
        // user is scrolling to the left from the first item to the fake 'item N'.
        // reposition offset to show the 'real' item N at the right end end of the collection view

        newIndexPath = [NSIndexPath indexPathForItem:([self.galleryArray count] -2) inSection:indexPath.section];

        self.currentIndexPath = newIndexPath;

        [self.bannerCollectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

    }


}

}

Michal Shatz
  • 1,416
  • 2
  • 20
  • 31