UICollectionView current visible cell index

后端 未结 16 1425
难免孤独
难免孤独 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: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)
        }
    }
    

提交回复
热议问题