Get Visible IndexPath UICollectionView in Swift

前端 未结 5 1884
-上瘾入骨i
-上瘾入骨i 2021-02-07 11:02

How to get visible IndexPath while scrolling in collectionView,I referred many link1,link2 But indexPathForCell is not suppor

5条回答
  •  时光取名叫无心
    2021-02-07 11:35

    try this

    On delegate

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

    Choice-2

    on Button Click

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

    Get visible All Items

    you can use indexPathsForVisibleRows

    Returns an array of index paths each identifying a visible row in the receiver.

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

提交回复
热议问题