title: UICollectionView within a UICollectionViewCell (Swift)
body: <p>I'm trying to put a <code>UICollectionView</code> within each reusable <code>UICollectionViewCell</code>. The <a href="http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell/" rel="nofollow noreferrer">Ash Furrow</a> method didn't work out too well for me, because using one <code>UICollectionViewController</code> class for the data source and delegate of two <code>UICollectionViews</code> does not work.</p>
  
  <p>My latest approach is to put the view of a <code>UICollectionViewController</code> inside each cell, as described in <a href="https://stackoverflow.com/q/14741951/2490299">this question</a> and <a href="https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html" rel="nofollow noreferrer">this doc</a>. However, when I go try to load the view, my app freezes. In the Xcode debug navigator, the CPU is at a constant 107% and the Memory approaches 1GB after a few seconds.</p>
  
  <p>In this example, I am trying to get a <code>ThumbnailCollectionViewController</code> in each <code>MainCollectionViewControllerCell</code>. The only thing in each <code>ThumbnailCollectionViewControllerCell</code> is an image of size 50x50.</p>
  
  <p>How is this done properly?</p>
  
  <p><strong>In MainCollectionViewController</strong></p>
  
  <pre><code>override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
  
      let thumbsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ThumbnailColllection") as! ThumbnailCollectionViewController
      self.addChildViewController(thumbsViewController)
      thumbsViewController.view.frame = cell.bounds
      cell.addSubview(thumbsViewController.view)
      thumbsViewController.didMoveToParentViewController(self)
  } 
  </code></pre>
  
  <p><strong>ThumbnailCollectionViewController</strong></p>
  
  <pre><code>let reuseIdentifier = "ThumbCell"
  let thumbnails = ["red", "green", "blue"]
  
  override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return thumbnails.count
  }
  
  override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
      let cellImage = UIImage(named: thumbnails[indexPath.row])
      let cellImageView = UIImageView(image: cellImage)
      cellImageView.frame = cell.bounds
      cell.addSubview(cellImageView)
  
      return cell
  }
  </code></pre>