title: Swift 3 - Reload UICollectionView inside the UITableViewCell
body: <p>I have a UICollectionView inside a UITableViewCell. You may refer the image at <a href="https://stackoverflow.com/questions/33867005/how-to-use-storyboard-quick-build-a-collectionview-inside-uitableviewcell">here</a></p>
  
  <p>I would like to reload the collectionView if any update happen.</p>
  
  <p>I have done some research and found this : </p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/35621863/how-to-reload-a-collectionview-that-is-inside-a-tableviewcell">how to reload a collectionview that is inside a tableviewcell</a></li>
  <li><a href="https://stackoverflow.com/questions/35908574/reloading-collection-view-inside-a-table-view-cell-happens-after-all-cells-in-ta">Reloading collection view inside a table view cell happens after all cells in table view have been loaded</a></li>
  <li><a href="https://stackoverflow.com/questions/33756419/uicollectionview-not-updating-inside-uitableviewcell">UICollectionView not updating inside UITableViewCell</a></li>
  </ol>
  
  <p>I called the <code>@IBOutlet weak var collectionView: UICollectionView!</code> from <code>UITableViewCell</code> to <code>UITableViewController</code> at <code>cellForRowAt</code>.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>var refreshNow: Bool = false
  
  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.allCardCell, for: indexPath) as! AllCardTableViewCell
  
          if refreshNow {
            cell.collectionView.reloadData()
            refreshNow = false
          }
  
          cell.collectionView.collectionViewLayout.invalidateLayout()
          return cell
      }
  </code></pre>
  
  <p>If the user click <code>Ok</code> on UIAlertAction :</p>
  
  <pre><code>let alert = UIAlertController(title: "Success", message: "Card successfully added", preferredStyle: .alert)
                  let action = UIAlertAction(title: "Ok", style: .default) { (action) in
                      self.refreshNow = true
                      self.tableView.reloadData()
                  }
                  alert.addAction(action)
                  self.present(alert, animated: true, completion: nil)
  </code></pre>
  
  <p>The reason why I put the <code>refreshNow</code> is to prevent the apps from lagging and slow. But still did not update if any changes happen.</p>
  
  <p>The problem is the collectionView did not refresh. But when I debug, it was went through the <code>cell.collectionView.reloadData()</code>.</p>
  
  <p>The update/changes only happen when I restart the apps. I want it to be so called <code>real-times</code> update.</p>
  
  <p>Any help is really appreciated and many thanks.</p>
  
  <p>Image credit: <a href="https://stackoverflow.com/questions/33867005/how-to-use-storyboard-quick-build-a-collectionview-inside-uitableviewcell">How to use StoryBoard quick build a collectionView inside UITableViewCell</a></p>