title: CollectionView Custom Cell Does Not Update Correctly When SearchController is Active
body: <p>I have a collectionview with a searchcontroller and a custom cell. When I make an edit in the cell while the search is not active everything gets updated with no problem. But when the searchcontroller is active my edits does not get reloaded correctly. Here are some of my codes. </p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      //SEARCH
      if searchController.isActive{
          return searchResults.count
      }else{
      return products.count
      }
      //SEARCH
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.ProductCell, for: indexPath) as? ProductCell {
  
          //SEARCH
          // Determine if we get the products from search result or the original array
          let searchItem = (searchController.isActive) ? searchResults[indexPath.item] : products[indexPath.item]
  
          cell.product = searchItem
          cell.delegate = self
  
          return cell
      }
      return UICollectionViewCell()
  }
  </code></pre>
  
  <p>I have a listener for populating the info in the cell for the collection and here is my code for the listener: </p>
  
  <pre><code>func setProductsListener(){
  
      listener = ProductsService.getProducts { [weak self] change, product in
  
          guard let self = self else { return }
          switch change.type {
          case .added:
              self.onDocumentAdded(change: change, product: product)
          case .modified:
              self.onDocumentModified(change: change, product: product)
          case .removed:
              self.onDocumentRemoved(change: change)
  
          @unknown default: break
          }
      }
  }
  </code></pre>
  
  <p>and here is where my changes gets reloaded to the collectionView: </p>
  
  <pre><code>    func onDocumentAdded(change: ProductChange, product: Product){
          let newIndex = Int(change.newIndex)
          products.insert(product, at: newIndex)
          collectionView.insertItems(at: [IndexPath(item: newIndex, section: 0)])
  
          // calculate and set title for cart subtotal
          cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
          cartBtn.sizeToFit()
      }
  
      func onDocumentModified(change: ProductChange, product: Product) {
  
          if change.newIndex == change.oldIndex {
  
              // Item changed, but remained in the same position
              let index = Int(change.newIndex)
              products[index] = product
  
              (collectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? ProductCell)?.product = product
  
              // calculate and set title for cart subtotal
              cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
              cartBtn.sizeToFit()
          } else {
  
              // Item changed and changed position
              let oldIndex = Int(change.oldIndex)
              let newIndex = Int(change.newIndex)
              products.remove(at: oldIndex)
              products.insert(product, at: newIndex)
  
              collectionView.moveItem(at: IndexPath(item: oldIndex, section: 0), to: IndexPath(item: newIndex, section: 0))
  
              // calculate and set title for cart subtotal
              cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
              cartBtn.sizeToFit()
          }
      }
  
      func onDocumentRemoved(change: ProductChange){
          let oldIndex = Int(change.oldIndex)
          products.remove(at: Int(oldIndex))
          collectionView.deleteItems(at: [IndexPath(item: oldIndex, section: 0)])
  
          // calculate and set title for cart subtotal
          cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
          cartBtn.sizeToFit()
      }
  </code></pre>
  
  <p>So here is what my UI looks like when the search controller is not active: </p>
  
  <p><a href="https://i.stack.imgur.com/bO4d5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bO4d5.png" alt="enter image description here"></a></p>
  
  <p>When I start searching and input the word "As.." (trying to search for Asparagus) I get this result: </p>
  
  <p><a href="https://i.stack.imgur.com/yd5MS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yd5MS.png" alt="enter image description here"></a></p>
  
  <p>So far I have no problem. But as soon as I try to click on "plus" or "minus" to change the quantity while the search controller is active; the wrong index also gets updated; look at the screenshot below. Not only index 0 gets updated for Asparagus; index 2 also gets updated to be the same item, Asparagus. </p>
  
  <p><a href="https://i.stack.imgur.com/GIJMG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GIJMG.png" alt="enter image description here"></a></p>
  
  <p>I am quite certain the problem is in the following code where once the modification is done; the wrong index also get updated when the search controller is active. Any idea how I can change the code below so the right index gets updated when the search controller is active? </p>
  
  <pre><code>func onDocumentModified(change: ProductChange, product: Product) {
  
      if change.newIndex == change.oldIndex {
  
          // Item changed, but remained in the same position
          let index = Int(change.newIndex)
          products[index] = product
  
          (collectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? ProductCell)?.product = product
  
          // calculate and set title for cart subtotal
          cartBtn.setTitle(subtotal.penniesToFormattedCurrency(), for: .normal)
          cartBtn.sizeToFit()
      }
  </code></pre>