Oritm
Oritm

Reputation: 2121

iPhone: Indexpath.row of UICollectionView

I try to remake the image gallery of iOS.

In the detail screen of an image i want to update the title of the navigationbar to like

3 of 19

Is there a way to get the current indexpath.row of the visible object and next to that update the image title when the UICollectionView stopped scrolling and update the '3 of 19' title.

Upvotes: 0

Views: 3430

Answers (2)

pabitranjan
pabitranjan

Reputation: 775

Implement below delegate method of UIScrollViewDelegate(as UICollectionViewDelegate is inherited from UIScrollViewDelegate).

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  NSInteger currentIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
  [self setTitle:[NSString stringWithFormat:@"%ld of %ld",(long)currentIndex + 1, (long)self.itemList.count]]; 
}

Upvotes: 3

Oritm
Oritm

Reputation: 2121

This code works fine!

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    BOPhotoCollectionViewCell* currentCell = ([[collectionView visibleCells]count] > 0) ? [[collectionView visibleCells] objectAtIndex:0] : nil;
    if(cell != nil){
        _index = [collectionView indexPathForCell:currentCell].row;
        [self updateTitle];
    }
}

Upvotes: 3

Related Questions