Reputation: 61
I am trying to perform a segue from a collection view which is inside a table view cell, the view looks like this
To explain it well, I have a table view, each row has a cell , inside the second row , I have a collection view which includes several products
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath)
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemTitle", for: indexPath)
return cell
case 2:
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
return cell
case 3:
let cell = tableView.dequeueReusableCell(withIdentifier: "SectionTitle", for: indexPath)
return cell
case 4:
let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
return cell
}
}
Inside my ItemCell class I have the following
@IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "singleItemCell", for: indexPath) as! singleItemCell
cell.itemImage.image = images[indexPath.row]
cell.itemImage.contentMode = .scaleAspectFit
return cell
}
I want to perform a segue such as when the user select a cell from the collection view, It takes him to another view controller. How can I do that ?
I tried performSegue but it does not allow me since I am in the cell class
Please any tips ?
Upvotes: 4
Views: 15217
Reputation: 711
protocol ProductCellDelegate: class {
func didChoosedProduct(item: ItemImage)
}
class ProductCell {
@IBOutlet weak var collectionView: UICollectionView!
// Your Model Array
var itemImages: [ItemImages] = [] {
didSet {
self.collectionView.reloadData()
}
// Must set the delegate to your viewcontroller object in tableviewcell cell for rowat indexpath method
weak var delegate: ProductCellDelegate?
override func awakeFromNib() {
self.collectionView.delegate = self
}
}
extension ProductCell: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedItem = self.itemImages[indexPath.row]
self.delegate?.didChoosedProduct(item: selectedItem)
}
}
Implement ProductCellDelegate
in your viewcontroller. Call performsegue to navigation.
Upvotes: 1
Reputation: 1383
In Your ItemCell class make a variable for referencing your ViewController like this:
var productVC:UIVIewController?
while loading TableView Cell set the reference to self like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 2:
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
cell.productVC = self
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
return cell
}
}
then implement the didSelect UICollectionView delegate method in ItemCell Class
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
productVC.performSegue(withIdentifier: "pass your segue identifier here..", sender: nil)
}
Upvotes: 1
Reputation: 2829
Implement UICollectionViewDataSource
and UICollectionViewDelegate
not in tableView cell, but in your ViewController
.
You can access to collectionView in cellForRowAtIndexPath
method, so you can assign it dataSource
and delegate
to self
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath)
return cell
case 1:
if let cell = tableView.dequeueReusableCell(withIdentifier: "ItemTitle", for: indexPath) as? ItemCell {
cell.collectionView?.delegate = self
cell.collectionView?.dataSource = self
return cell
}
...
}
}
And from ViewController
you can make performSegue
method
Upvotes: 0