Get visible IndexPath UICollectionView in Swift

How to get visible IndexPath while scrolling in collectionView , I mentioned a lot of link1 , link2 But indexPathForCell not supported in Swift.

+7
ios swift uicollectionview
source share
4 answers

Have you tried the delegate function?

 public func indexPathsForVisibleItems() -> [NSIndexPath] 

or

 collectionView.indexPathsForVisibleItems() 

they should give you what you wanted.

+12
source share

try it

On delegate

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

Choice-2

at the touch of a button

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

Show all items

you can use indexPathsForVisibleRows

Returns an array of pointer paths, each of which identifies the visible string in the receiver.

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

Swift, a safe way to get visible elements :

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

You can use the UICollectionView method as:

 let indexPath = NSIndexPath(item: value1, section: value2) 
0
source share

All Articles