Changing the numberOfItemsInSection of a collectionView, from another collectionView didSelectSelectItemAt indexPath.

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 ?

Accepted Reply

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

Replies

Well, you will return to this in a few days, and you may "magically" find it.


Just one point: what happens if you revert order:

putting lines 31-39 AFTER lines 41-52 ?


Wish you good.

Following up on the changes, I get this error in the console; (That actually happens from time to time, I really hope so.)


[UICollectionView] Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (2) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out). - will perform reloadData. UICollectionView instance: <UICollectionView: 0x7fa54789d400; frame = (0 742; 375 70); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000337660>; layer = <CALayer: 0x600000dcf6e0>; contentOffset: {0, 0}; contentSize: {375, 133.75}; adjustedContentInset: {0, 0, 34, 0}; layout: <UICollectionViewFlowLayout: 0x7fa54663f890>; dataSource: <"appName".PhotoViewController: 0x7fa54666bc50>>; currentUpdate: [UICollectionViewUpdate - 0x7fa546662ef0: old:<UICollectionViewData: 0x60000343dc00> new<UICollectionViewData: 0x600003433b80> items:<(

"I(0,0)"

)>]


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == resetConfirmCollectionView {
     
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ResetConfirmCellID", for: indexPath) as! ResetConfirmCell
     
      let config = UIImage.SymbolConfiguration(pointSize: 40, weight: .semibold)
     
      if indexPath.row == 0 {
       
        guard let cellImage = UIImage(systemName: "multiply", withConfiguration: config) else {
          print("SystemImage not available")
          return cell
        }
        cell.resetConfirmImage = cellImage
        return cell
      }
      if dataPassed?.row == 1 {
        let cellCount = resetConfirmCollectionView.visibleCells.count
        let item = cellCount - 1
        let cellIndexPath = IndexPath(item: item, section: 0)
        resetConfirmCollectionView.insertItems(at: [cellIndexPath])
        guard let cellImage = UIImage(systemName: "pencil", withConfiguration: config) else {
          print("SystemImage not available")
          return cell
        }
        cell.resetConfirmImage = cellImage
        return cell
      }
      if indexPath.row == 1 {
        print("Passed on row 1")
        guard let cellImage = UIImage(systemName: "checkmark", withConfiguration: config) else {
          print("SystemImage not available")
          return cell
        }
        cell.resetConfirmImage = cellImage
        return cell
      }
    }