UICollectionView current visible cell index

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

    In this thread, There are so many solutions that work fine if cell takes full screen but they use collection view bounds and midpoints of Visible rect However there is a simple solution to this problem

        DispatchQueue.main.async {
            let visibleCell = self.collImages.visibleCells.first
            print(self.collImages.indexPath(for: visibleCell))
        }
    

    by this, you can get indexPath of the visible cell. I have added DispatchQueue because when you swipe faster and if for a brief moment the next cell is shown then without dispactchQueue you'll get indexPath of briefly shown cell not the cell that is being displayed on the screen.

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

    Just want to add for others : for some reason, I didnt not get the cell that was visible to the user when I was scrolling to previous cell in collectionView with pagingEnabled.

    So I insert the code inside dispatch_async to give it some "air" and this works for me.

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        dispatch_async(dispatch_get_main_queue(), ^{
                UICollectionViewCell * visibleCell= [[self.collectionView visibleCells] objectAtIndex:0];
    
    
                [visibleCell doSomthing];
            });
    }
    
    0 讨论(0)
  • 2020-11-29 17:25

    indexPathsForVisibleItems might work for most situations, but sometimes it returns an array with more than one index path and it can be tricky figuring out the one you want. In those situations, you can do something like this:

    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];
    

    This works especially well when each item in your collection view takes up the whole screen.

    Swift version

    let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
    let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
    let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)
    
    0 讨论(0)
  • 2020-11-29 17:30

    The method [collectionView visibleCells] give you all visibleCells array you want. Use it when you want to get

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
            NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
            NSLog(@"%@",indexPath);
        }
    }
    

    Update to Swift 5:

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        for cell in yourCollectionView.visibleCells {
            let indexPath = yourCollectionView.indexPath(for: cell)
            print(indexPath)
        }
    }
    
    0 讨论(0)
提交回复
热议问题