[英]Getting first Cell/Item of UICollectionView while scrolling - iOS
I have a UICollectionView
with 1 row 1 column. 我有一个带有1行1列的
UICollectionView
。 I want to get the first item ( indexPath
basically) when I am scrolling it horizontally. 我想水平滚动时获得第一项(基本上是
indexPath
)。
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
. 例如,我在
UICollectionView
水平显示了100个项目,并且当我从右向左滚动时,无论哪个项目是第一个可见的项目,我都需要它的indexPath
。 Which means the indexPath
will be constantly changing while its scrolling. 这意味着
indexPath
在滚动时将不断变化。
How to achieve this? 如何实现呢? Please Guide.
请指导。 Thanks!
谢谢!
I think you need implement UIScrollView
delegate method and get visible cell inside 我认为您需要实现
UIScrollView
委托方法并在其中获取可见的单元格
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
UICollectionViewCell *firstCell = [[collectionView visibleCells] firstObject];
NSIndexPath *firstIndexPath = [collectionView indexPathForCell: firstCell];
NSLog(@"%@", firstIndexPath);
}
Hope this help 希望这个帮助
collectionView.indexPathsForVisibleItems()
returns an array of NSIndexPath
for visible cells. collectionView.indexPathsForVisibleItems()
返回可见单元格的NSIndexPath
数组。 You can get the left most one by sorting them by NSIndexPath.item
. 您可以通过按
NSIndexPath.item
对它们进行排序来获得最左边的一个。
Edti : To be notified while scrolling, implement the UIScrollViewDelegate
method scrollViewDidScroll(_: UIScrollView)
. Edti :要在滚动时得到通知,请实现
UIScrollViewDelegate
方法scrollViewDidScroll(_: UIScrollView)
。 Remember that UICollectionView
is a subclass of UIScrollView
. 请记住,
UICollectionView
是UIScrollView
的子类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.