title: UICollectionView cells with Images inside UITableView prototype
body: <p>UPDATE: I solved my primary issue of correct images not loading until scrolling on the collectionView. I added a collectionView.reloadData() to the tableView:cellForRowAtIndexPath. I also made some changes to pre-load the sequence array, instead of building it while scrolling through the table (tableView:cellForRowAtIndexPath).</p>
  <p>Added the updates to GitHub if you are interested.<br />
  <a href="https://github.com/Druiced/OpenDeck" rel="nofollow noreferrer">https://github.com/Druiced/OpenDeck</a></p>
  <p>I will follow-up once I figure out how to prevent the App from crashing when a dynamic value is placed in the return (if i set this to 15, the app will not crash):</p>
  <pre><code>func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return count(Array(sequenceArray[collectionView.tag])) / 2
  }
  </code></pre>
  <p>ORIGINAL POST:
  request for some guidance.</p>
  <p>This tutorial helped me realize this must have to do with my DataSource/Delegate. The author builds the cell with addSubview instead of taking advantage of the Xcode prototype cell, which seems like a cool thing, so I'm trying to do it.
  <a href="http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell" rel="nofollow noreferrer">http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell</a></p>
  <p>Any criticism about my approach or failure to follow best practices is welcome.</p>
  <p>Each cell in the table has a UICollectionView. Each cell in the Collection View displays an image in order of the saved &quot;Sequence&quot; string. example: &quot;ADKDQDJDTD&quot; link up to AD.png KD.png QD.png JD.png TD.png</p>
  <p><img src="https://i.stack.imgur.com/GUiqT.png" alt="enter image description here" /></p>
  <p>I have two issues I can't seem to get past.</p>
  <blockquote>
  <ul>
  <li>numberOfItemsInSection gets whacky when the number of cards is driven by the array length (return handArray.count / 2). If I place a fixed number the app will work, but not very slick.</li>
  <li>When the table first comes up, the correct cards do not display until I scroll up and down the table. It also appears the data for each CollectionView is crossing paths as the wrong cards show up when scrolling up and down rapidly.</li>
  </ul>
  </blockquote>
  <p>I'm almost positive this has to do with how my datasource is setup.</p>
  <p>DeckTableViewController.swift</p>
  <pre><code>import UIKit
  import Parse
  
  var deviceID: String?
  var noRefresh: Bool?
  var sequenceArray: Array&lt;Character&gt;?
  
  class DeckTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource {
  var handArray: Array&lt;Character&gt;!
  var timeLineData:NSMutableArray = NSMutableArray()
  
  override func viewDidLoad() {
      super.viewDidLoad()
      noRefresh = false
      deviceId = UIDevice.currentDevice().identifierForVendor.UUIDString
  }
  
  override func viewDidAppear(animated: Bool) {
      if noRefresh == false {
          loadData()
          noRefresh = true
      }
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
      return 1
  }
  
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return timeLineData.count
  }
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
      let cell:DeckTableViewCell = tableView.dequeueReusableCellWithIdentifier(&quot;Cell&quot;, forIndexPath: indexPath) as! DeckTableViewCell
      
      let deck:PFObject = timeLineData.objectAtIndex(indexPath.row) as! PFObject
  
      cell.collectionView.dataSource = self
      cell.collectionView.delegate = self
      
      let sequenceTemp = deck.objectForKey(&quot;Sequence&quot;) as! String
      handArray = Array(sequenceTemp)
      cell.sequenceId.setTitle(deck.objectId, forState: UIControlState.Normal)
      cell.cardCountLabel.text = &quot;\((count(sequenceTemp)/2))&quot;
      
      // Date to String Stuff
      var dateFormatter = NSDateFormatter()
      dateFormatter.dateFormat = &quot;(MM-dd) hh:mm:ss&quot;
      cell.timeLabel.text = dateFormatter.stringFromDate(deck.updatedAt!)
      
      let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
      layout.itemSize = CGSizeMake(99, 140)
      layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
      
      cell.collectionView.collectionViewLayout = layout
  
      return cell
  }
  
  
  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return handArray.count / 2
  }
  
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
      
      let cell:TableCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(&quot;Cell&quot;, forIndexPath: indexPath) as! TableCollectionViewCell
      var bcolor : UIColor = UIColor.orangeColor()
      cell.layer.borderColor = bcolor.CGColor
      cell.layer.borderWidth = 2
      cell.layer.cornerRadius = 3
      
      var firstLetter: Character!
      var secondLetter: Character!
  
      //Building card file names from Sequence data
      if (indexPath.row * 2) + 1 &lt;= handArray.count {
  
          firstLetter = handArray[indexPath.row * 2]
          secondLetter = handArray[indexPath.row * 2 + 1]
          let imageNameString = &quot;\(firstLetter)\(secondLetter).png&quot;
          let front = UIImage(named: imageNameString)
          cell.ImageView.backgroundColor = UIColor.orangeColor()
          cell.ImageView.image = front
  
      }
  
      return cell
  }
  </code></pre>
  <p>DeckTableViewCell.swift</p>
  <pre><code>import UIKit
  
  class DeckTableViewCell: UITableViewCell, UITextViewDelegate {
  
      @IBOutlet var collectionView: UICollectionView!
      @IBOutlet var sequenceId: UIButton!
      @IBOutlet var timeLabel: UILabel!
      @IBOutlet var cardCountLabel: UILabel!
  
      override func awakeFromNib() {
          super.awakeFromNib()
          // Initialization code
      }
  
      override func setSelected(selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
  
      }
  }
  </code></pre>
  <p>TableCollectionViewCell.swift</p>
  <p>import UIKit</p>
  <pre><code>class TableCollectionViewCell: UICollectionViewCell {
      
      @IBOutlet var ImageView: UIImageView!
      
  }
  </code></pre>
  <p>For this example I set (return handArray.count / 2) to a 10 and loaded 3 sequences.
  The number in the top center represents the number of cards for each row.
  Notice the CollectionView does not update with the right cards, it's picking up data from the other CollectionViews. IF I add bunch more sequences to this mix, when scrolling up and down, the correct cards WILL populate SOMETIMES, but unpredictable.</p>
  <p><img src="https://i.stack.imgur.com/epMbi.png" alt="enter image description here" /></p>
  <p>Thanks for any suggestions, I'm happy to go back to the drawing board. Cheers</p>