title: multiple UICollectionviews inside a UItabelview with sections - swift
body: <p>Basically, I am trying to obtain something similar to the result of this : <a href="http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell" rel="noreferrer">http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell</a></p>
  
  <p>how ever, i'm want 2 collectionsViews, 1 in first section and another in 2nd section</p>
  
  <p>how do i achieve this ?
  i'm bashing my head in here... each time i try to set delegate and datasource it fails and says i need to register a nib
  Also i have no idea how i separate the collection views in it's cellForRowAtIndexPath, how can i see which one is loading ?
  how can i get it to load properly so that first section has self.genre and 2nd section has the self.radioStat</p>
  
  <p>this is the error i get when i try to set delegate and datasource for the 2nd time </p>
  
  <p>**Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CollectionViewCell34 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
  **</p>
  
  <p><strong>tableViewCellCollectionView.swift</strong></p>
  
  <pre><code>import Foundation
  import UIKit
  let collectionViewCellIdentifier: NSString = "CollectionViewCell"
  class tableViewCellCollectionView: UITableViewCell  {
  
  var collectionView: UICollectionView!
  
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
      super.init(style: style, reuseIdentifier: reuseIdentifier)
  
      var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
      layout.sectionInset = UIEdgeInsetsMake(4, 5, 4, 5)
      layout.minimumLineSpacing = 5
      layout.itemSize = CGSizeMake(91, 91)
      layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
      self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
      self.collectionView.registerClass(collectionViewCellTableView.self, forCellWithReuseIdentifier: "CollectionViewCell")
      self.collectionView.registerClass(collectionViewCellTableView.self, forCellWithReuseIdentifier: collectionViewCellIdentifier)
      self.collectionView.backgroundColor = UIColor.lightGrayColor()
      self.collectionView.showsHorizontalScrollIndicator = false
      self.contentView.addSubview(self.collectionView)
      self.layoutMargins = UIEdgeInsetsMake(10, 0, 10, 0)
  
  }
  
  required init(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
  }
  
  override func layoutSubviews() {
      super.layoutSubviews()
      let frame = self.contentView.bounds
      self.collectionView.frame = CGRectMake(0, 0.5, frame.size.width, frame.size.height - 1)
  }
  
  func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol&lt;UICollectionViewDelegate,UICollectionViewDataSource&gt;, index: NSInteger) {
      self.collectionView.dataSource = delegate
      self.collectionView.delegate = delegate
      self.collectionView.tag = index
      self.collectionView.reloadData()
  }
  }
  </code></pre>
  
  <p><strong>tableViewCellCollectionView34.swift</strong></p>
  
  <pre><code>import Foundation
  import UIKit
  let collectionViewCellIdentifier34: NSString = "CollectionViewCell34"
  class tableViewCellCollectionView2: UITableViewCell  {
  var collectionView2: UICollectionView!
  
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
      super.init(style: style, reuseIdentifier: reuseIdentifier)
  
  
      var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
      layout.sectionInset = UIEdgeInsetsMake(4, 5, 4, 5)
      layout.minimumLineSpacing = 1
      layout.itemSize = CGSizeMake(86, 54)
      layout.scrollDirection = UICollectionViewScrollDirection.Vertical
      self.collectionView2 = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
      self.collectionView2.registerClass(collectionViewCellTableView2.self, forCellWithReuseIdentifier: collectionViewCellIdentifier34)
      self.collectionView2.backgroundColor = UIColor.lightGrayColor()
      self.collectionView2.showsHorizontalScrollIndicator = false
      self.contentView.addSubview(self.collectionView2)
      self.layoutMargins = UIEdgeInsetsMake(10, 0, 10, 0)
  
  
  }
  
  required init(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
  }
  
  override func layoutSubviews() {
      super.layoutSubviews()
      let frame = self.contentView.bounds
      self.collectionView2.frame = CGRectMake(0, 0.5, frame.size.width, frame.size.height - 1)
  }
  
  func setCollectionViewDataSourceDelegate2(dataSourceDelegate delegate: protocol&lt;UICollectionViewDelegate,UICollectionViewDataSource&gt;, index: NSInteger) {
      self.collectionView2.dataSource = delegate
      self.collectionView2.delegate = delegate
      self.collectionView2.tag = index
      self.collectionView2.registerClass(collectionViewCellTableView2.self, forCellWithReuseIdentifier: collectionViewCellIdentifier34)
      self.collectionView2.reloadData()
  } 
  
  }
  </code></pre>
  
  <p><strong>Player.swift</strong></p>
  
  <pre><code>   extension Player: UICollectionViewDataSource,UICollectionViewDelegate {
  
  
  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return self.radioStat.count
  }
  
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
  
          let cell: collectionViewCellTableView = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier, forIndexPath: indexPath) as collectionViewCellTableView
          var rowData: String = self.genre.objectAtIndex(indexPath.row) as String
          cell.title.text = rowData as String
  
          let cell2: collectionViewCellTableView2 = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier2, forIndexPath: indexPath) as collectionViewCellTableView2
          let userPost: String  = self.radioStat.objectAtIndex(indexPath.row) as String
          cell2.pinImage.image = UIImage(named: userPost)
  
  
  
  
      let collectionViewArray = self.sourceArray[collectionView.tag] as NSArray
  
  
  
      return cell
  }
  extension Player {
  
  
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return 1
  }
  
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
     var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as UITableViewCell
  
  
      if (indexPath.section == 0){
          var cell2: tableViewCellCollectionView = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier, forIndexPath: indexPath) as tableViewCellCollectionView
          return cell2
      }else if (indexPath.section == 1){
          let cell3: tableViewCellCollectionView2 = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier2, forIndexPath: indexPath) as tableViewCellCollectionView2
          return cell3
      }
      return cell
  
  }
  
  func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
      if (indexPath.section == 0){
      if let collectionCell: tableViewCellCollectionView = cell as? tableViewCellCollectionView{
      collectionCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, index: indexPath.row)
      }
      }
      if (indexPath.section == 1){
      if let collectionCell2: tableViewCellCollectionView2 = cell as? tableViewCellCollectionView2{
          collectionCell2.setCollectionViewDataSourceDelegate2(dataSourceDelegate: self, index: indexPath.row)
      }
      }
  
  
      }
  
  
  func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {
  
      if (indexPath.section == 0){
          return 50
      }
  
      if (indexPath.section == 1){
          return 450
      }else {
          return 100
      }
  
  }
  
  func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
              return sourceArray.count
          }
  }
  </code></pre>