Basically, I am trying to obtain something similar to the result of this : http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
how ever, i'm want 2 collectionsViews, 1 in first section and another in 2nd section
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
this is the error i get when i try to set delegate and datasource for the 2nd time
**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' **
tableViewCellCollectionView.swift
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<UICollectionViewDelegate,UICollectionViewDataSource>, index: NSInteger) {
self.collectionView.dataSource = delegate
self.collectionView.delegate = delegate
self.collectionView.tag = index
self.collectionView.reloadData()
}
}
tableViewCellCollectionView34.swift
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<UICollectionViewDelegate,UICollectionViewDataSource>, index: NSInteger) {
self.collectionView2.dataSource = delegate
self.collectionView2.delegate = delegate
self.collectionView2.tag = index
self.collectionView2.registerClass(collectionViewCellTableView2.self, forCellWithReuseIdentifier: collectionViewCellIdentifier34)
self.collectionView2.reloadData()
}
}
Player.swift
extension Player: UICollectionViewDataSource,UICollectionViewDelegate {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.radioStat.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> 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) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> 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) -> CGFloat {
if (indexPath.section == 0){
return 50
}
if (indexPath.section == 1){
return 450
}else {
return 100
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sourceArray.count
}
}
You need to know which (tableView) section your collectionView
relates to. One way to do this would be to subclass UICollectionView and add a tvSection
property to it - in the same way that Ash Furrow uses AFIndexedCollectionView
to add an index
property.
But you seem to be using the collectionView tag
in place of his index
, presumably to avoid subclassing. If so, a similar dodge would be to use the tag
of the tableViewCell's contentView
to indicate which section the cell is in. Amend the tableView cellForRowAtIndexPath
method to do this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> 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
cell2.contentView.tag = indexPath.section
return cell2
} else if (indexPath.section == 1){
let cell3: tableViewCellCollectionView2 = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier2, forIndexPath: indexPath) as tableViewCellCollectionView2
cell3.contentView.tag = indexPath.section
return cell3
}
cell.contentView.tag = indexPath.section
return cell
}
Because the collectionView is added as a subview of the contentView
, you can determine the (tableView) section for a given collectionView using superview.tag
. So modify your collectionView's cellForItemAtIndexPath
to test this:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let section = collectionView.superview.tag
if section == 0 {
let cell: collectionViewCellTableView = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier, forIndexPath: indexPath) as collectionViewCellTableView
// Now configure from the data...
var rowData: String = self.genre.objectAtIndex(indexPath.row) as String
cell.title.text = rowData as String
return cell
else {
let cell2: collectionViewCellTableView2 = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier2, forIndexPath: indexPath) as collectionViewCellTableView2
// Now configure from the data...
let userPost: String = self.radioStat.objectAtIndex(indexPath.row) as String
cell2.pinImage.image = UIImage(named: userPost)
return cell2
}
}
This will need some polishing; in particular I'm not sure how you wish to map your data (sourceArray
, genre
and radioStat
) to the tableView rows and collectionView items. (I've taken a guess based on your existing code). But this should give you something to work with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With