title: UICollectionView - Select all cells doesn't update properly
body: <p>I have a button which selects all cells in the collectionview. Once clicked, the button function changes so that all cells will be de-selected upon pressing it again.</p>
  
  <p>So far so good.. But<br>
  1) When you select all cells with the button, scroll a bit down and to the top again<br>
  2) Then de-select all cells with the button, and select all cells with the button again<br>
  3) And start scrolling down, some cells (mostly 1-2 complete rows, later cells are fine again) are not properly updated, so they <em>don't</em> appear with the selected state which is a different background color. Seems like an issue with dequeueReusableCell, but I can't wrap my head around it..</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {    
      if cell.isSelected {
          cell.backgroundColor = UIColor.green
      } else {
          cell.backgroundColor = UIColor.white
      }
  
      if cell.viewWithTag(1) != nil {
          let cellTitle = cell.viewWithTag(1) as! UILabel
          cellTitle.text = String(indexPath.row)
      }
  }
  
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      if let cell = collectionView.cellForItem(at: indexPath) {
          cell.backgroundColor = UIColor.green
  
          selectedCells.append(indexPath.row)
      }
  }
  
  func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
      if let cell = collectionView.cellForItem(at: indexPath) {
          cell.backgroundColor = UIColor.white
  
          selectedCells.removeObject(indexPath.row)
      }
  }
  </code></pre>
  
  <p>And the action method for handling button clicking</p>
  
  <pre><code>@IBAction func selectButtonTapped(_ sender: Any) {
      if isSelectAllActive {
          // Deselect all cells
          selectedCells.removeAll()
  
          for indexPath: IndexPath in collectionView!.indexPathsForSelectedItems! {
              collectionView!.deselectItem(at: indexPath, animated: false)
              collectionView(collectionView!, didDeselectItemAt: indexPath)
  
              let cell: UICollectionViewCell
              cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CVCell", for: indexPath)
          }
  
          selectButton.title = "Select all"
          isSelectAllActive = false
      } else {
          // Select all cells
          for i in 0 ..&lt; collectionView!.numberOfItems(inSection: 0) {
              collectionView!.selectItem(at: IndexPath(item: i, section: 0), animated: false, scrollPosition: UICollectionViewScrollPosition())
              collectionView(collectionView!, didSelectItemAt: IndexPath(item: i, section: 0))
          }
  
          selectedCells.removeAll()
  
          let indexPaths: [IndexPath] = collectionView.indexPathsForSelectedItems!
          for item in indexPaths {
              selectedCells.append(item.row)
          }
          selectedCells.sort{$0 &lt; $1}
  
          selectButton.title = "Select none"
          isSelectAllActive = true
      }
  }
  </code></pre>
  
  <p>And for completion the array extension for removing an object</p>
  
  <pre><code>extension Array where Element : Equatable {
      mutating func removeObject(_ object : Iterator.Element) {
          if let index = self.index(of: object) {
              self.remove(at: index)
          }
      }
  }
  </code></pre>
  
  <p>Complete Xcode project can be found here: <a href="https://www.dropbox.com/s/uaj1asg43z7bl2a/SelectAllCells.zip" rel="nofollow noreferrer">https://www.dropbox.com/s/uaj1asg43z7bl2a/SelectAllCells.zip</a><br>
  Used Xcode 9.0 beta 1, with iOS11 Simulator/iPhone SE</p>
  
  <p>Thanks for your help!</p>