0

hello everyone is definitely nonsense but I am confused when I try to select a cell from my collectionView.

I need to save the selected indexPath and show it when my collectionView is reloaded.

When I select a cell (after the collectionView has been reloaded) I display 2 cells selected instead of one It appears that the new selected indexPath is not updated immediately and therefore I display the new indexPath and the old indexPath until I scroll through the cells ..

Is the problem in cellForItemAt?

This is the code

override func layoutSubviews() {
    super.layoutSubviews()

    daySelectedIndexPath = IndexPath(item: currentDay-1, section: 0)

    daysCv.selectItem(at: daySelectedIndexPath, animated: false, scrollPosition: .centeredHorizontally)

}

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: daysreuseCellID, for: indexPath) as! DaysCell

       cell.isSelected = daySelectedIndexPath == indexPath ? true : false

       return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    guard daySelectedIndexPath != indexPath else { return }

    collectionView.deselectItem(at: daySelectedIndexPath!, animated: true)

    daySelectedIndexPath = indexPath

}
kAiN
  • 2,559
  • 1
  • 26
  • 54

0 Answers0