在Swift中获取可见的IndexPath UICollectionView

如何在scrolling collectionView同时获取visible IndexPath ,我提到了很多link1 , link2,但是indexPathForCell不支持indexPathForCell

你尝试过委托function吗?

 public func indexPathsForVisibleItems() -> [NSIndexPath] 

要么

 collectionView.indexPathsForVisibleItems() 

这些必须给你你想要的。

尝试这个

在委托

 func scrollViewDidEndDecelerating(scrollView: UIScrollView) { for cell in yourCollectionViewname.visibleCells() as [UICollectionViewCell] { let indexPath = yourCollectionViewname.indexPathForCell(cell as UICollectionViewCell) NSLog("%@", indexPath) } } 

select-2

点击button

  var point : CGPoint = sender.convertPoint(CGPointZero, toView:yourCollectionViewname) var indexPath =yourCollectionViewname!.indexPathForItemAtPoint(point) 

显示所有项目

你可以使用indexPathsForVisibleRows

返回一组索引path,每个索引path标识接收器中的可见行。

  • (NSArray *)indexPathsForVisibleItems;
 var visible: [AnyObject] = yourCollectionViewname.indexPathsForVisibleItems var indexpath: NSIndexPath = (visible[0] as! NSIndexPath) 

Swift, 更安全的方式来显示项目

 if let indexPaths = self.collectionView.indexPathsForVisibleItems { //Do something with an indexPaths array. }