简体   繁体   English

滚动时获取UICollectionView的第一个单元格/项-iOS

[英]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 . 请记住, UICollectionViewUIScrollView的子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionView - 滚动到第一个单元格 - UICollectionView - Scrolling to first cell iOS中的UICollectionView单元格滚动问题 - UICollectionView cell scrolling issue in ios iOS:UICollectionView-滚动时出现抽动 - iOS: UICollectionView - Jerks while scrolling UICollectionView和Selected Cell在滚动时丢失选择 - UICollectionView and Selected Cell loses selection while scrolling iOS swift UICollectionView 在滚动超过第一个屏幕后失去单元格约束 - iOS swift UICollectionView loses cell constraints after scrolling past first screen full UICollectionView:单元格删除后滚动到带有动画的最后一个单元格时出现故障 - UICollectionView: glitches while scrolling to the last cell with animation after cell deletion 在第一个单元格加载到UICollectionView中之前获取UICollectionViewCell - Getting a UICollectionViewCell before the first cell is loaded in the UICollectionView 为什么只更新第一个单元格中的图像,而uicollectionview中的其余单元格不更新。 iOS Swift - Why is only image in first cell updated while the rest of the cells in uicollectionview not updated. ios swift 滚动到 UICollectionView 中的下一个单元格 - Scrolling to next cell in UICollectionView UICollectionView:激活,滚动到第一响应者并将其分配给远程单元 - UICollectionView: activating, scrolling to, and assigning first responder to distant cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM