title: Reloading collection view inside a table view cell happens after all cells in table view have been loaded
body: <p>I am trying to implement a collection view inside each table view cell in a table view, but am having trouble getting the collection view to reload at the right time. It looks like the collection view reloads itself after all the table view cells have been loaded, not each time a new cell is dequeued to the table view as I'm trying to make it do.</p>
  <pre><code>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      
      let cell = tableView.dequeueReusableCellWithIdentifier(&quot;LWTableViewCell&quot;) as! LWTableViewCell
      
      cell.collectionView.delegate = self
      cell.collectionView.dataSource = self
      
      if dataIsReady == 1 {
          setIndex = indexPath.row
          print(&quot;in table view cellForRowAtIndexPath, setting setIndex at \(setIndex)&quot;)
          cell.collectionView.reloadData()
      }
      return cell
  }
  
  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      
      if dataIsReady == 1 {
          print(&quot;In collection view numberOfItemsInSection setIndex is \(setIndex)&quot;)
          return self.model.sets[setIndex].subsets!.count
      }
      else { return 0 }   
  }
  </code></pre>
  <p>In terminal, I get the following:</p>
  <blockquote>
  <p>in table view cellForRowAtIndexPath, setting setIndex at 0</p>
  <p>in table view cellForRowAtIndexPath, setting setIndex at 1</p>
  <p>in table view cellForRowAtIndexPath, setting setIndex at 2</p>
  <p>In collection view numberOfItemsInSection setIndex is 2</p>
  <p>In collection view numberOfItemsInSection setIndex is 2</p>
  <p>In collection view numberOfItemsInSection setIndex is 2</p>
  </blockquote>
  <p>whereas I would expect to see the following order of events (given that the collection view reload method is/should be called each time a new table view cell is dequeued).</p>
  <blockquote>
  <p>in table view cellForRowAtIndexPath, setting setIndex at 0</p>
  <p>In collection view numberOfItemsInSection setIndex is 0</p>
  <p>in table view cellForRowAtIndexPath, setting setIndex at 1</p>
  <p>In collection view numberOfItemsInSection setIndex is 1</p>
  <p>in table view cellForRowAtIndexPath, setting setIndex at 2</p>
  <p>In collection view numberOfItemsInSection setIndex is 2</p>
  </blockquote>
  <p>Any suggestions on why this behavior is happening, and how to potentially fix it would be much appreciated!</p>
  <p>I have looked through some other questions on the topic, and am aware there are some tutorials on the topic (e.g., <a href="https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/" rel="nofollow noreferrer">https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/</a>), but before starting to do things with a different approach, I'd like to understand why the above doesn't seem to work / what could be done to make it work.</p>