UICollectionView current visible cell index

后端 未结 16 1420
难免孤独
难免孤独 2020-11-29 16:52

I am using UICollectionView first time in my iPad application. I have set UICollectionView such that its size and cell size is same, means only onc

相关标签:
16条回答
  • 2020-11-29 17:17

    UICollectionView current visible cell index: Swift 3, 4 and 5+

    var visibleCurrentCellIndexPath: IndexPath? {
        for cell in self.collectionView.visibleCells {
            let indexPath = self.collectionView.indexPath(for: cell)
            return indexPath
         }
            
         return nil
    }
    

    As an Extension:

    extension UICollectionView {
      var visibleCurrentCellIndexPath: IndexPath? {
        for cell in self.visibleCells {
          let indexPath = self.indexPath(for: cell)
          return indexPath
        }
        
        return nil
      }
    }
    

    Usage:

    if let indexPath = collectionView.visibleCurrentCellIndexPath { 
       /// do something
    }
    
    0 讨论(0)
  • 2020-11-29 17:18

    It will probably be best to use UICollectionViewDelegate methods: (Swift 3)

    // Called before the cell is displayed    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        print(indexPath.row)
    }
    
    // Called when the cell is displayed
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        print(indexPath.row)
    }
    
    0 讨论(0)
  • 2020-11-29 17:18

    This is old question but in my case...

    - (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
        _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.firstObject].row;
    }
    
    - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        _m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.lastObject].row;
    }
    
    0 讨论(0)
  • 2020-11-29 17:19

    For Swift 3.0

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let visibleRect = CGRect(origin: colView.contentOffset, size: colView.bounds.size)
        let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
        let indexPath = colView.indexPathForItem(at: visiblePoint)
    }
    
    0 讨论(0)
  • 2020-11-29 17:21

    Also check this snippet

    let isCellVisible = collectionView.visibleCells.map { collectionView.indexPath(for: $0) }.contains(inspectingIndexPath)
    
    0 讨论(0)
  • 2020-11-29 17:23

    Swift 5:

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        var visibleRect = CGRect()
    
        visibleRect.origin = collectionView.contentOffset
        visibleRect.size = collectionView.bounds.size
    
        let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
    
        guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return } 
    
        print(indexPath)
    }
    

    Working Answers Combined In Swift 2.2 :

     func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    
            var visibleRect = CGRect()
    
            visibleRect.origin = self.collectionView.contentOffset
            visibleRect.size = self.collectionView.bounds.size
    
            let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))
    
            let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)
    
            guard let indexPath = visibleIndexPath else { return } 
            print(indexPath)
    
        }
    
    0 讨论(0)
提交回复
热议问题