UICollectionView current visible cell index

后端 未结 16 1418
难免孤独
难免孤独 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:06

    try this, it works. (in the example below i have 3 cells for example.)

        func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let visibleRect = CGRect(origin: self.collectionView.contentOffset, size: self.collectionView.bounds.size)
        let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))
        let visibleIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)
        if let v = visibleIndexPath {
            switch v.item {
            case 0: setImageDescription()
                break
            case 1: setImageConditions()
                break
            case 2: setImageResults()
                break
            default: break
            }
        }
    
    0 讨论(0)
  • 2020-11-29 17:08

    converting @Anthony's answer to Swift 3.0 worked perfectly for me:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        var visibleRect = CGRect()
        visibleRect.origin = yourCollectionView.contentOffset
        visibleRect.size = yourCollectionView.bounds.size
        let visiblePoint = CGPoint(x: CGFloat(visibleRect.midX), y: CGFloat(visibleRect.midY))
        let visibleIndexPath: IndexPath? = yourCollectionView.indexPathForItem(at: visiblePoint)
        print("Visible cell's index is : \(visibleIndexPath?.row)!")
    }
    
    0 讨论(0)
  • 2020-11-29 17:11

    For completeness sake, this is the method that ended up working for me. It was a combination of @Anthony & @iAn's methods.

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
          CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
          CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
          NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
          NSLog(@"%@",visibleIndexPath);
    }
    
    0 讨论(0)
  • 2020-11-29 17:15

    You can use scrollViewDidEndDecelerating: for this

    //@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
    
       - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
            for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
                NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
                NSUInteger lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
                NSLog(@"visible cell value %d",lastIndex);
            }
    
        }
    
    0 讨论(0)
  • 2020-11-29 17:15

    Swift 3 & Swift 4:

    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[1])
    }
    

    If you want to show actual number than you can add +1

    0 讨论(0)
  • 2020-11-29 17:16

    Swift 3.0

    Simplest solution which will give you indexPath for visible cells..

    yourCollectionView.indexPathsForVisibleItems 
    

    will return the array of indexpath.

    Just take the first object from array like this.

    yourCollectionView.indexPathsForVisibleItems.first
    

    I guess it should work fine with Objective - C as well.

    0 讨论(0)
提交回复
热议问题