iOS / Swift: The selection of the cell in a collectionview is not working vertically with scrollToItem:


When I use this code, it works well for the horizontal position, but not for the vertical position. The horizontal position is always good, the vertical is random.
How to position on the exact cell ?

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) {
        let indexPath2 = IndexPath(row: row + 1, section: section + 1)

        // Assurez-vous que l'indexPath est valide
        guard indexPath2.section < self.slotCollectionView.numberOfSections &&
              indexPath2.row < self.slotCollectionView.numberOfItems(inSection: indexPath2.section) else {
            return
        }

        self.slotCollectionView.selectItem(at: indexPath2, animated: true, scrollPosition: [])

        // Utilisez scrollToItem avec positionnement centré verticalement et horizontalement
        self.slotCollectionView.scrollToItem(at: indexPath2, at: [.centeredVertically, .centeredHorizontally], animated: true)
    }

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img