I am trying to navigate to another ViewController from Collectionview cell which is inside Tableview cell.
I am trying to achieve using delegate method but it's not navigating to intended viewcontroller.
Here is code that i have developed so far. I am using xib setup here.
// ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,CustomTableViewCellDelegate {
@IBOutlet weak var tableView: UITableView!
var customTableViewCell = CustomTableViewCell()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
customTableViewCell.delegate = self
tableView.delegate = self
tableView.dataSource = self
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.navigationBar.barTintColor = UIColor.black
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.tintColor = UIColor(red: 211/255, green: 86/255, blue: 50/255, alpha: 1.0)
}
//Delegate method
func passTheCurrent(tableIndex: Int, collectionViewIndex: Int) {
print("collectionViewIndex \(collectionViewIndex)")
let selectpile = ObjectSceneViewCtrl()
self.navigationController?.pushViewController(selectpile, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
return customCell
}
}
Here is CustomTableViewCell where i am defining delegate methods. I am calling delegate function inside collectionview didSelectItemAt method. But delegate is returning nil.
import UIKit
protocol CustomTableViewCellDelegate {
func passTheCurrent(tableIndex: Int, collectionViewIndex: Int)
}
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
var delegate: CustomTableViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.register(UINib.init(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension CustomTableViewCell : UICollectionViewDelegate {}
extension CustomTableViewCell : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath as IndexPath) as! CollectionViewCell
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.passTheCurrent(tableIndex: 0, collectionViewIndex: indexPath.item)
}
}
When i set break point, delegate is returning nil. What is wrong with this setup. please help me out.