cells must be retrieved by calling - dequeueReusableCellWithReuseIdentifier:forIndexPath - uitableview

Problem encountered:
the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier - cells must be retrieved by calling - dequeueReusableCellWithReuseIdentifier:forIndexPath:"
In my TableViewCell, I have a CollectionViewCell.
import UIKit
protocol CollectionViewTableViewCellDelegate: AnyObject {
func collectionViewTableViewCellDidTapCell(_ cell: CollectionViewTableViewCell, viewModel: TitlePreviewViewModel)
}
class CollectionViewTableViewCell: UITableViewCell {
static let identifier = "CollectionViewTableViewCellId"
weak var delegate: CollectionViewTableViewCellDelegate?
private var titles: [Title] = [Title]()
private let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 140, height: 200)
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.register(TitleCollectionViewCell.self, forCellWithReuseIdentifier: TitleCollectionViewCell.identifier)
return collectionView
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(collectionView)
collectionView.delegate = self
collectionView.dataSource = self
}
required init?(coder: NSCoder) {
fatalError()
}
override func layoutSubviews() {
super.layoutSubviews()
collectionView.frame = contentView.bounds
}
public func configure(with titles: [Title]) {
self.titles = titles
DispatchQueue.main.async { [weak self] in
self?.collectionView.reloadData()
}
}
I extend this class CollectionViewTableViewCell as follows:
extension CollectionViewTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TitleCollectionViewCell.identifier, for: indexPath) as? TitleCollectionViewCell else {
return UICollectionViewCell()
}
//-- get the specific data from the pass-in Data
guard let model = titles[indexPath.row].poster_path else {
return UICollectionViewCell()
}
//- pass data to TitleCollectionViewCell
cell.configure(with: model)
return cell
}
In my Viewcontroller:
I have UItableView to retrieve the cell data as follows:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: CollectionViewTableViewCell.identifier, for: indexPath) as? CollectionViewTableViewCell else {
return UITableViewCell()
}
cell.delegate = self
Base on the Problem: for UItableView,there is no such dequeue method: dequeueReusableCellWithReuseIdentifier.
How to solve this problem?
Thanks. Please kindly help me.

Related

How to push view controller on a button tap in nested collection view cell swift

I have a collection view inside a table view and a button in the collection view cell. I want to push another vc when that button is pressed.
I tried creating a delegate in the collection view cell, but the collection view cell has a cellforitem method in the table cell, so the delegate cannot be declared in the main view controller.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
setUpTableView()
}
func setUpTableView() {
let tableView = UITableView()
view.addSubview(tableView)
tableView.register(
HomeRecommendStoreTableViewCell.self,
forCellReuseIdentifier: HomeRecommendStoreTableViewCell.identifier
)
tableView.register(
UITableViewCell.self,
forCellReuseIdentifier: "cell"
)
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = .black
tableView.showsVerticalScrollIndicator = false
tableView.separatorStyle = .none
tableView.contentInsetAdjustmentBehavior = .never
self.tableView = tableView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: HomePopularTableViewCell.identifier, for: indexPath) as? HomePopularTableViewCell else { return tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)}
cell.selectionStyle = .none
return cell
}
}
class TableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
static let identifier = "HomeRecommendStoreTableViewCell"
private var collectionView: UICollectionView!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = .black
setUpCollectionView()
}
required init?(coder: NSCoder) {
fatalError()
}
func setUpCollectionView() {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(
frame: .zero,
collectionViewLayout: layout
)
layout.scrollDirection = .horizontal
collectionView.register(
HomeRecommendStoreCollectionViewCell.self,
forCellWithReuseIdentifier: HomeRecommendStoreCollectionViewCell.identifier
)
collectionView.register(
UICollectionViewCell.self,
forCellWithReuseIdentifier: "cell"
)
collectionView.clipsToBounds = false
collectionView.backgroundColor = .black
collectionView.showsHorizontalScrollIndicator = false
collectionView.delegate = self
collectionView.dataSource = self
contentView.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(40)
make.leading.equalToSuperview().offset(20)
make.trailing.equalToSuperview()
make.bottom.equalToSuperview().inset(80)
}
self.collectionView = collectionView
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: HomeRecommendStoreCollectionViewCell.identifier, for: indexPath
) as? HomeRecommendStoreCollectionViewCell else {
return collectionView.dequeueReusableCell(
withReuseIdentifier: "cell", for: indexPath
)
}
return cell
}
}
class CollectionViewCell: UICollectionViewCell {
static let identifier = "HomeRecommendStoreCollectionViewCell"
private lazy var listButton: UIButton = {
let button = UIButton()
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
func setUp() {
listButton.addTarget(self, action: #selector(onTap), for: .touchUpInside)
}
#objc func onTap() {
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
First you have to make a property in tableViewCell:
weak var parent:ViewController?
Then in viewController you have to use cell for row of tableView:
cell.parent = self
Then create same property in collectionViewCell:
weak var parent:ViewController?
And use collectionView func cell for item at:
cell.parent = parent
And use that parent inside your button func:
#objc func onTap() {
let destinationVC = NextViewController()
parent?.navigationController.pushViewController(destinationVC,animated:true)
}
Since the delegate is inside the collectionView, you can get a callback in TableView cell and then with another delegate you can get the call back in your ViewController.
Or else you can also use notifications in order to get callbacks.
Or closures can also be used.

Why doesn't go CollectionViewCell?

I noticed that Topics CollectionViewCell was not gone when I debugged. Why is that?
the cells are all correct but the collectionview is not available.
The link of the project is below. you can also look from there.
here I define cell.
extension ArticlesVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CollectionTableViewCell.identifier, for: indexPath) as! CollectionTableViewCell
cell.configure(with: models)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
}
struct Model {
let text: String
let imageName: String
init(text: String, imageName: String) {
self.text = text
self.imageName = imageName
}
}
Here I define CollectionTableViewCell
class CollectionTableViewCell: UITableViewCell {
static let identifier = "CollectionTableViewCell"
var collectionView: UICollectionView?
var models = [Model]()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
collectionView?.register(TopicsCollectionViewCell.self, forCellWithReuseIdentifier: TopicsCollectionViewCell.identifier)
collectionView?.delegate = self
collectionView?.dataSource = self
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
contentView.addSubview(collectionView!)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
collectionView?.frame = contentView.bounds
}
func configure(with models: [Model]) {
self.models = models
collectionView?.reloadData()
}
}
extension CollectionTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return models.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TopicsCollectionViewCell.identifier, for: indexPath) as! TopicsCollectionViewCell
cell.configure(with: models[indexPath.row])
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 250, height: 250)
}
}
**This TopicsCollectionViewCell **
class TopicsCollectionViewCell: UICollectionViewCell {
static let identifier = "TopicsCollectionViewCell"
let titleLabel: UILabel = {
let label = UILabel()
label.text = "label"
return label
}()
let photoImage: UIImageView = {
let imageView = UIImageView()
return imageView
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(titleLabel)
contentView.addSubview(photoImage)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
photoImage.frame = CGRect(x: 3,
y: 3,
width: contentView.width - 6,
height: 150)
titleLabel.frame = CGRect(x: 3,
y: photoImage.bottom + 5,
width: contentView.width - 6,
height: contentView.height - photoImage.height - 6)
}
public func configure(with model: Model) {
print(model)
self.titleLabel.text = model.text
self.photoImage.image = UIImage(named: model.imageName)
}
}
This link is project with github
During execution those lines are useless as collectionView? is nil
collectionView?.register(TopicsCollectionViewCell.self, forCellWithReuseIdentifier: TopicsCollectionViewCell.identifier)
collectionView?.delegate = self
collectionView?.dataSource = self
You need to reorder the code by initing the collectionView first
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
contentView.addSubview(collectionView!)
collectionView?.register(TopicsCollectionViewCell.self, forCellWithReuseIdentifier: TopicsCollectionViewCell.identifier)
collectionView?.delegate = self
collectionView?.dataSource = self

Using dictionary values to populate collectionView's label inside of a tableView

So I have a collectionView inside of a tableView. I would like to use the values from my array to populate each labels text inside each collectionViewCell. If I print the code below in collectionView cellForItemAt I get the below (see picture) (and obviously index out of range):
Print(array[indexPath.row].details.values)
So using the example from the photo, how can I get the following:
Outer tableViewCell (1)
CollectionViewCell (1)
label - "1"
CollectionViewCell (2)
label - "3"
CollectionViewCell (3)
label - "5"
Outer tableViewCell (2)
CollectionViewCell (1)
label - "7"
Also as you may of noticed its the array is not in order, is it possible to reorder so its:
["1": 1, "2": 3, "3": 5]
Any help is greatly appreciated, many thanks!!
My Array:
Based on your requirements here is the code
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
// Global Variable
var tableView: UITableView!
var dataArray = [["2": 3, "1": 1, "3": 5], ["1":7]]
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: self.view.bounds)
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath as IndexPath) as! TableViewCell
// Passing data to cellection cell
cell.cellData = dataArray[indexPath.row]
cell.backgroundColor = UIColor.groupTableViewBackground
return cell
}
}
class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView: UICollectionView!
var cellData = [String: Int]()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView.backgroundColor = UIColor.clear
self.addSubview(collectionView)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// MARK: UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: CollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath as IndexPath) as! CollectionCell
cell.textLable.text = String(Array(cellData)[indexPath.row].value) // Please check here is some casting
cell.backgroundColor = .black
return cell
}
}
class CollectionCell: UICollectionViewCell {
let textLable: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
label.textColor = .white
label.translatesAutoresizingMaskIntoConstraints = true
label.textAlignment = .center
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupLayout()
}
private func setupLayout() {
addSubview(textLable)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I hope it solves your issue.
Result
If I understood it correctly you have an array with this, slightly odd, format
array = [[["2": 3, "1": 1, "3": 5]], [["1":7]]]
Since the collection view is inside a table view I assume you have implemented the NSTableViewDataSource and then you can save the current row for the table view as a property in tableView: viewFor: and thus get the array to use with that property. Since you have an array within an array we need to get the first item in that array and then filter that item (a dictionary) where the key matches the current indexPath.row
let row = String(indexPath.row + 1)
let item = array[currentTableViewRow][0].filter {$0.key = row}
if !item.isEmpty {
label.text = item.value
}
Conver it to Array With this :
Array(array[indexPath.row].details.values)
And print this value You get proper array in proper order.
I hope it will help you,
Thank you.

Collection View inside Table View Cell

I want to create collection view inside the table view cell for horizontal scrolling of elements. But I have the problem how to fill right my collection views. I need a way to connect collection views with my data.
How can I get and loop through Table View Cells to find needed collection view and return the cells with data?
below is a general example of how you can pass data to collection views inside your table view cell. Also this link is a youtube tutorial on this subject.
Models:
class ListOfParents: NSObject {
var parents:[Parent]?
}
class Parent: NSObject {
var children: [Child]?
static func fetchParents(_ completionHandler: #escaping (ListOfParents) -> ()) {
//fetch parents data
}
}
class Child: NSObject {
}
TableView cell:
class CustomTableViewController: UITableViewController {
var listOfParents: ListOfParents?
override func viewDidLoad() {
super.viewDidLoad()
Parent.fetchparents { (listOfParents) in
self.listOfParents = listOfParents
}
tableView.register(CustomParentCell.self, forCellReuseIdentifier: "tableCell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let parentsCount = listOfParents?.parents?.count else {return 0}
return parentsCount
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! CustomParentCell
cell.parent = listOfParents?.parents?[indexPath.item]
return cell
}
}
Parent cell:
class CustomParentCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
var parent: Parent? {
didSet {
// set child value here
}
}
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
return collectionView
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CustomChildCell.self, forCellWithReuseIdentifier: "childCellID")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let childrenCount = parent?.children?.count else {return 0}
return childrenCount
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "childCellID", for: indexPath) as! CustomChildCell
cell.child = parent?.children?[indexPath.item]
return cell
}
}
Child cell:
class CustomChildCell: UICollectionViewCell {
var child: Child?
}

multiple UICollectionviews inside a UItabelview with sections - swift

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.

Resources