0

I am getting a index of data as int. I m getting position of my data which can be 1 or 2 or 3 or 4 .. so on. How can i use these int to fetch colletionviewcell from collectionview.

on each scrolling i m getting index of cell as int for e.g initally it will be 0 for next swipe the number will be 1, next swipe nos will be 2 and so on.

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
            let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
            let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing
            var offset = targetContentOffset.pointee
            let index = (offset.x + scrollView.contentInset.left) / cellWidthIncludingSpacing
            let roundIndex = round(index)
            offset = CGPoint(x: roundIndex * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
            targetContentOffset.pointee = offset

for cell in collectionView.visibleCells as! [CaurosalCollectionViewCell] {
            // do something

            print(cell.title.text)
        }
    }

expected output

2 Answers2

1

You can use yourCollectionView?.indexPathsForVisibleItems but it returns an array. So you have to compute which indexPath is visible at the current contentOffset.

Something like:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let visibleRect = CGRect(origin: yourCollectionView.contentOffset, size: yourCollectionView.bounds.size)
    let visiblePoint = CGPoint(x: CGFloat(visibleRect.midX), y: CGFloat(visibleRect.midY))
    let visibleIndexPath: IndexPath? = yourCollectionView.indexPathForItem(at: visiblePoint)
}

Edit

You need to look into UICollectionViewLayout and specifically layoutAttributesForItem(at indexPath: IndexPath). You can update the layout based on the current IndexPath without going through the scrollview delegate.

If you wish to use the scollview delegate I believe this question was better answered here: How to create a centered UICollectionView like in Spotify's Player

ahbou
  • 4,710
  • 23
  • 36
  • i have updated my func in question i need to fetch cell based on roundIndex . I am getting "Cannot convert value of type 'CGFloat' to expected argument type 'CGPoint'" i dont have CGPoint i am adjusting the horizontal collectionview cell in the center and i need to increase its z-positions – feroz siddiqui Oct 11 '17 at 13:46
  • can u check my code how can i fetch cell based on roundIndex variable – feroz siddiqui Oct 11 '17 at 14:04
1

You can create indexpath from this index, providing the section (I am taking it 0). If no section then it is zero. Further, passing this indexpath to fetch the cell, now you have to typecast the cell into required type then you can use it

 let indexpath = IndexPath(row: index, section: 0)
 guard let cell = collectionView.cellForItem(at: indexpath) as? CaurosalCollectionViewCell else { return }