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

61 Views Asked by At

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)
}

I precise that the height of the cell are different from a cell to another, and the widths of the cells are the same.

0

There are 0 best solutions below