title: How to track changes in an array and collectionview.reloaddata()
body: <p>i'm experiencing issues with reloading data in my collectionview. I have an array populating the collectionview. However whenever i add more items to the array it doesn't show up unless I reloaddata(). What is the best way to have the collectionview reloaddata whenever it sees changes in the array, whether is removing or adding new elements? Thanks in advance! Edit: Added Code.</p>
  
  <p>class CheckoutController: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout  {</p>
  
  <pre><code>var inventoryTabController: InventoryTabController?
  
  override init(frame: CGRect){
      super.init(frame: frame)
      setupViews()
  
  }
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  
  
  func loadItems() -&gt; [Item]? {
      return NSKeyedUnarchiver.unarchiveObject(withFile: Item.ArchiveURL.path) as? [Item]
  }
  
  func saveItems() {
      let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(checkout, toFile: Item.ArchiveURL.path)
      if !isSuccessfulSave {
          print("Failed to save items...")
      }
  }
  
  func addItem(item: Item) {
      items.append(item)
      collectionView.reloadData()
  }
  
  func editItem(item: Item, index: Int) {
      items[index] = item
      collectionView.reloadData()
  }
  
  func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  
      // Identify which segue is occuring.
      if segue.identifier == "ShowDetail" {
          let itemDetailViewController = segue.destination as! AddItemController
  
          // Get the cell that generated this segue.
          if let selectedItemCell = sender as? InventoryCell {
              let indexPath = collectionView.indexPath(for: selectedItemCell)!
              let selectedItem = items[indexPath.row]
              itemDetailViewController.item = selectedItem
          }
      }
      else if segue.identifier == "AddItem" {
          print("Adding new meal.")
      }
  }
  
  lazy var collectionView: UICollectionView = {
      let layout = UICollectionViewFlowLayout()
      let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
  
      cv.dataSource = self
      cv.delegate = self
  
      cv.backgroundColor = UIColor.rgb(r: 247, g: 247, b: 247)
      return cv
  }()
  
  let cellId = "cellId"
  let paynow = "paynow"
  func setupViews() {
      backgroundColor = .brown
  
      addSubview(collectionView)
  
      addConstraintsWithFormat("H:|[v0]|", views: collectionView)
      addConstraintsWithFormat("V:|[v0]|", views: collectionView)
      collectionView.indicatorStyle = UIScrollViewIndicatorStyle.white
  
      collectionView.register(PayNowCell.self, forCellWithReuseIdentifier: paynow)
  
      collectionView.register(CheckoutCell.self, forCellWithReuseIdentifier: cellId)
  }
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return checkout.count //init number of cells
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      if indexPath.item == 0 {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "paynow", for: indexPath) as! PayNowCell //init cells
      return cell
      }else{
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CheckoutCell //init cells
      print("Printing this \(checkout.count)")
      cell.item = checkout[indexPath.item]
      return cell
      }
  
  }
  
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      //let item = items[indexPath.item]
      //inventoryController?.showItemDetailForItem(item: item, index: indexPath.item)
      print("selected")
      print(indexPath.item)
      collectionView.reloadData()
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
      if indexPath.item == 0 {
          return CGSize(width:frame.width, height: 100) //each cell dimensions
      }else{
          return CGSize(width:frame.width, height: 150) //each cell dimensions
      }
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -&gt; CGFloat {
      return 0
  }
  </code></pre>
  
  <p>}</p>