Hi, I have 2 separate collectionViews and I've been trying to change the numberOfItemsInSection of "collectionViewA" according to the didSelectItemAt indexPath of "collectionViewB". Let's say I have 2 items already setup in "collectionViewA" but if I select "collectionViewB" indexPath.row = 1, I would like to have 3 items in "collectionViewA", and if I select "collectionViewB" indexPath.row = 2, I would like to have 4 items in "collectionViewA". As far as I can see, this is possible but I couldn't find the relevant info online. Any idea on this please ?
Have you checked that you did not allow multiple cell selection in the collectionView ?
If so, the new selection (1) is added to the initial selection (0) ; creating the problem.
In viewDidLoad, you could set
collectionView.
allowsMultipleSelection = false
Another way to avoid, is to deselect all cells in DidSelect before resetting to the indexPath.
Here is a way to deseelct all, with a simple extension:
https://stackoverflow.com/questions/38430668/uicollectionview-how-to-deselect-all
extension UICollectionView {
func deselectAllItems(animated: Bool) {
guard let selectedItems = indexPathsForSelectedItems else { return }
for indexPath in selectedItems { deselectItem(at: indexPath, animated: animated) }
}
}