title: How to expand multiple UICollectionViewCells on tap
body: <p>I'm trying to build a collectionView that can expand multiple cells after they've been selected/tapped or collapsed when they are deselected, everything works fine when the cells remain on the screen, but once the expanded cells go off screen, I get unexpected behaviour.</p>
  
  <p>For example if I select a cell with IndexPath 0 and then scroll down, tap on cell with IndexPath of 8, scroll back to cell with IndexPath 0 (it's already collapsed), I would tap on it and scroll back to the cell with IndexPath 8 and tap on it again it expands + cell with IndexPath 10 would expand too.</p>
  
  <p>The CollectionView has been implemented programmatically as well the UICollectionViewCell has been subclassed.</p>
  
  <p>ViewController that holds the UICollectionView:</p>
  
  <pre><code>import UIKit
  
  class CollectionViewController: UIViewController {
  
      // MARK: - Properties
      fileprivate var collectionView: UICollectionView!
  
      var manipulateIndex: NSIndexPath? {
          didSet {
              collectionView.reloadItems(at: collectionView.indexPathsForSelectedItems!)
          }
      }
  
      // MARK: - Lifecycle
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
          layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
  
          collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
          collectionView.dataSource = self
          collectionView.delegate = self
          collectionView.register(CustomCell.self, forCellWithReuseIdentifier: "Cell")
          collectionView.backgroundColor = UIColor.white
          self.view.addSubview(collectionView)
      }
  
  }
  
  // MARK: - UICollectionViewDataSource
  extension CollectionViewController: UICollectionViewDataSource {
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 13
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
  
          cell.textLabel.text = "\(indexPath.item)"
          cell.backgroundColor = UIColor.orange
  
          return cell
      }
  }
  
  // MARK: - UICollectionViewDelegate
  extension CollectionViewController: UICollectionViewDelegate {
  
      func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -&gt; Bool {
          let cell = collectionView.cellForItem(at: indexPath) as! CustomCell
          cell.expanded = !cell.expanded
  
          manipulateIndex = indexPath as NSIndexPath
  
          return false
      }
  }
  
  // MARK: - UICollectionViewDelegateFlowLayout
  extension CollectionViewController: UICollectionViewDelegateFlowLayout {
  
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  
          if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell {
              if cell.expanded == true {
                  return CGSize(width: self.view.bounds.width - 20, height: 300)
              }
  
              if cell.expanded == false {
                  return CGSize(width: self.view.bounds.width - 20, height: 120.0)
              }
          }
  
          return CGSize(width: self.view.bounds.width - 20, height: 120.0)
  
      }
  }
  </code></pre>
  
  <p>And the subclassed custom UICollectionViewCell:</p>
  
  <pre><code>import UIKit
  
  class CustomCell: UICollectionViewCell {
  
      var expanded: Bool = false
      var textLabel: UILabel!
  
      override init(frame: CGRect) {
          super.init(frame: frame)
  
          textLabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height/3))
          textLabel.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
          textLabel.textAlignment = .center
          contentView.addSubview(textLabel)
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  }
  </code></pre>
  
  <p>Please help and thank you so much to the amazing person, who can help me out! :)</p>