UICollectionView current visible cell index

后端 未结 16 1441
难免孤独
难免孤独 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.

提交回复
热议问题