Sha Sha - 4 months ago 24
Objective-C Question

Getting first Cell/Item of UICollectionView while scrolling - iOS

I have a

UICollectionView
with 1 row 1 column. I want to get the first item (
indexPath
basically) when I am scrolling it horizontally.

For example, I have got 100 items displayed horizontally in my
UICollectionView
and when I scroll from right to left, whichever item is the first one visible, I need its
indexPath
.
Which means the
indexPath
will be constantly changing while its scrolling.

How to achieve this? Please Guide. Thanks!

Answer

collectionView.indexPathsForVisibleItems() returns an array of NSIndexPath for visible cells. You can get the left most one by sorting them by NSIndexPath.item.

Edti: To be notified while scrolling, implement the UIScrollViewDelegate method scrollViewDidScroll(_: UIScrollView). Remember that UICollectionView is a subclass of UIScrollView.