title: collectionView cellForItemAt not being called
body: <p>I've got a <code>UITableView</code> where each prototype cell has a <code>UICollectionView</code> in it. This collection view is supposed to be a grid of images. I'm very new to Swift, and have googled for hours (and read plenty of StackOverflow articles), and cannot seem to find the correct answer to this question.</p>
  
  <p>How come collectionView cellForItemAt is not being called?</p>
  
  <p>I see that my collectionView <code>numberOfItemsInSection</code> method, and collectionView <code>sizeForItemAt</code> methods get called. I've made sure to assign my <code>TableViewCell</code> as the delegate and dataSource of the collectionView. I did this both in the Interface Builder with outlets, and again in my custom TableViewCell's <code>awakeFromNib</code> method with</p>
  
  <pre><code>cardImagesCollection.delegate = self
  cardImagesCollection.dataSource = self
  </code></pre>
  
  <p>No matter what I do, I cannot seem to get this to call it. I made sure that I had good constraints set up because that burned me once on the tableViewCells. </p>
  
  <p>For the life of me, I cannot get it to load up the collectionView.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Specifically, I'm using Xcode 9.4.1 and Swift 4.1.</p>
  
  <p>Thanks</p>
  
  <h1>Edit - Adding sample code</h1>
  
  <h2>HomeViewController: UIViewController</h2>
  
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "LatestCardCell", for: indexPath) as! LatestCardsTableViewCell
  
      cell.backgroundColor = UIColor.clear
      cell.card = cards[indexPath.row]
      return cell
  }
  </code></pre>
  
  <h2>LatestCardsTableViewCell: UITableViewCell</h2>
  
  <pre><code>@IBOutlet weak var cardImagesCollection: UICollectionView!
  
  var card: Card! {
      didSet {
          titleLabel.attributedText = FontUtil.attributedCardTitleText(string: card.title)
          descriptionLabel.attributedText = FontUtil.attributedCardDescriptionText(string: card.description)
      }
  }
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      print("count")
      return card.imageUrls.count
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      print("here")
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LatestCardsImage", for: indexPath as IndexPath) as! LatestCardImageCell
  
      //let url = card.imageUrls[indexPath.row]
      //cell.imageView.image = UIImage(named: "test")
  
      cell.backgroundColor = UIColor.blue
      return cell
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
      return CGSize(width: 150, height: 150)
  }
  </code></pre>
  
  <h2>LatestCardImageCell: UICollectionViewCell</h2>
  
  <pre><code>@IBOutlet weak var imageView: UIImageView!
  </code></pre>
  
  <h1>Edit 2: Summary of suggestions I've tried</h1>
  
  <p>People have suggested setting the dataSource and delegate for the CollectionView inside the <code>cellForRowAt</code> call where I build the CollectionView:</p>
  
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "LatestCardCell", for: indexPath) as! LatestCardsTableViewCell
  
      cell.cardImagesCollection.delegate = cell
      cell.cardImagesCollection.dataSource = cell
      cell.backgroundColor = UIColor.clear
      cell.card = cards[indexPath.row]
      return cell
  }
  </code></pre>
  
  <p>This did not change anything. They also suggested adding the following inside of that cellForRowAt call right after setting cell.card:</p>
  
  <pre><code>cell.cardImagesCollection.reloadData()
  </code></pre>
  
  <p>This also did not change anything.</p>
  
  <p>I've put breakpoints and logging in my TableViewCell's custom class and I can see that <code>numberOfItemsInSection</code> gets calls once per TableViewCell, and it returns the correct number (this number varies between TableViewCell). I then also see <code>sizeForItemAt</code> get called the exact amount of times that <code>numberOfItemsInSection</code> returns. I do not, however, see the <code>cellForItemAt</code> method get called ever.</p>
  
  <p>Here's the logging:</p>
  
  <pre><code>numberOfItemsInSection: 6
  sizeForItemAt: Section 0 Row 0
  sizeForItemAt: Section 0 Row 1
  sizeForItemAt: Section 0 Row 2
  sizeForItemAt: Section 0 Row 3
  sizeForItemAt: Section 0 Row 4
  sizeForItemAt: Section 0 Row 5
  numberOfItemsInSection: 2
  sizeForItemAt: Section 0 Row 0
  sizeForItemAt: Section 0 Row 1
  numberOfItemsInSection: 4
  sizeForItemAt: Section 0 Row 0
  sizeForItemAt: Section 0 Row 1
  sizeForItemAt: Section 0 Row 2
  sizeForItemAt: Section 0 Row 3
  </code></pre>
  
  <p>Some have suggested making sure to define <code>cellForItemAt</code> by using autocomplete as opposed to copying and pasting, which is how I did it in the first place. I also have my custom TableViewCell set up with the following protocols <code>UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout</code> and it does not complain about a lack of <code>cellForItemAt</code> method. I currently have the delegate and dataSource set in the InterfaceBuilder and that is what is giving me the method calls I'm seeing.</p>
  
  <p>I've tried calling <code>reloadData()</code> on the collectionView in multiple places (the second I set the <code>cell.card</code> in my <code>cellForRowAt</code>, in my <code>didSet</code> call, and even the <code>awakeFromNib</code> method in the custom TableViewCell. Nothing works.</p>
  
  <p>Other aspects of the TableCell render, but not this image collection. I've hardcoded a size for the CollectionView cells in the InterfaceBuilder, and even overrode sizeForItemAt to always return <code>CGSize(width: 150, height: 150)</code>, just to be sure that the issue isn't having a cell that's too small to be visible. The tableView cell is set to auto height with <code>latestCardsTableView.rowHeight = UITableViewAutomaticDimension</code>, and I've added long text to my labels to ensure that they do grow in size with the text automatically.</p>
  
  <p>Does anyone have any other ideas? Are there things I can screenshot from the InterfaceBuilder, or some Swift code I can write to force some AutoLayout stuff? I'm open to anything.</p>
  
  <h1>Edit 3: A glimmer of hope</h1>
  
  <p>I've found that if I give my UITableViewCell an explicit large height, the UICollectionView does show up and does call <code>cellForItemAt</code>. So, how do I set my constraints so that the UICollectionView forces the UITableViewCell larger, given that I've got <code>rowHeight = UITableViewAutomaticDimension</code> set on my TableView?</p>
  
  <p><a href="https://i.stack.imgur.com/4OwQK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4OwQK.png" alt="Collection View Constriants"></a></p>