title: Add Item in UICollectionView From Another Another CollectionView (Swift)
body: <p>i have two collection view in one view controller. Top collection view is empty and i want that when i select any item in lower collection view this item add in top collection view.the problem is when i select item in lower collection view application crashes and i get this error :</p>
  
  <p>reason: 'Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (0), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'</p>
  
  <p>there is my code, how can solve this problem?</p>
  
  <pre><code>  override func viewDidLayoutSubviews() {
  
          super.viewDidLayoutSubviews()
  
          let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
          self.collectionView.collectionViewLayout = flowLayout
  
          flowLayout.estimatedItemSize = CGSize(width: collectionView.bounds.width/3, height:32)
          flowLayout.minimumLineSpacing = 2
          flowLayout.minimumInteritemSpacing = 2
  
          let flowLayout1 = self.topCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
          self.topCollectionView.collectionViewLayout = flowLayout1
  
          flowLayout1.estimatedItemSize = CGSize(width: collectionView.bounds.width/3, height:32)
          flowLayout1.minimumLineSpacing = 2
          flowLayout1.minimumInteritemSpacing = 2
  
          self.collectionView.collectionViewLayout.invalidateLayout()
          self.topCollectionView.collectionViewLayout.invalidateLayout()
  
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
  
  
          if collectionView == self.collectionView {
  
              return foodData.count 
          }
  
          else  {
          return selectFood.count
  
          }
  
      }
  
      func numberOfSections(in collectionView: UICollectionView) -&gt; Int {
          return 1
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LCell", for: indexPath)as! CollectionLabelCell
  
          if collectionView == self.collectionView {
  
              cell.titleLabel.text = foodData[indexPath.row]
  
          }
          else if collectionView == self.topCollectionView {
  
              if selectFood.count != 0 {
  
              cell.titleLabel.text = selectFood[indexPath.row]
  
              }
          }
  
          return cell
  
      }
  
  
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  
          if collectionView == self.collectionView {
          self.selectFood.append(foodData[indexPath.row])
  
              self.topCollectionView.performBatchUpdates({ () -&gt; Void in
  
                  self.topCollectionView.reloadData()
  
  
                  }, completion:nil)
              } 
      }
  
      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  
              return CGSize(width: collectionView.bounds.width/3, height:32)
      }
  </code></pre>