从 tableview 中的 Collection Cell 执行 segue

Perform segue from CollectionCell inside tableview

如何从 tableview 中的 collectioncell 执行 segue?我在 tableview 中创建了一个 collectionview,我想执行从 collectioncell 到另一个 veiwcontroller 的 segue。每个单元格都有自己的 segue。例如," if indexpath.row = 0 perform segue to identifier "A" if indexpath.row == 1 perferom segue to identifier "B"。谢谢,这是我的代码。

   import UIKit

   protocol SegueSelectionDelegate: class {
    
   func didSelect()
    
   }

   class ProductCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
   weak var delegate: SegueSelectionDelegate?
    
    
       var productArray:[(names:String,image:String)] =
           []
    
        @IBOutlet weak var collectionView: UICollectionView!{
          didSet{
              self.collectionView.delegate = self
              self.collectionView.dataSource = self
              
              self.collectionView.register(UINib(nibName: "CategoriesTwoCollCell", bundle: nil), 
              forCellWithReuseIdentifier: "cell")
              self.collectionView.reloadData()
          }
      }
    
          override func awakeFromNib() {
           super.awakeFromNib()
        // Initialization code
        
               productArray.append((names: "Bicycles", image: "KIA"))
               productArray.append((names: "Motorbikes", image: "Lotus"))
               productArray.append((names: "Fabric Safe",  image: "Mini"))
               productArray.append((names: "gas",  image: "Nissan"))
               productArray.append((names: "gas",  image: "Mercedes"))

               
               }

                override func setSelected(_ selected: Bool, animated: Bool) {
                super.setSelected(selected, animated: animated)
        
    

                 // Configure the view for the selected state
                }
    
                 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 
 section: Int) -> Int {
        
           return productArray.count
        
         }
         
         func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
           let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoriesTwoCollCell
           cell.titleProductlbl.text = self.productArray[indexPath.row].names
          //   cell.renlbl.text = self.productArray[indexPath.row].rent
           cell.imageView.image = UIImage(named:self.productArray[indexPath.row].image)
         
           return cell
         }
       
       func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
           
              return CGSize(width: 85, height: 90)
         }
    
            func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
      
           delegate?.didSelect()
       
        }

       }


 

     class MainTableViewController: UIViewController,MainTableViewCellDelegate ,SegueSelectionDelegate {

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier:"productcell1" , for: indexPath) as! ProductCell
                
                cell.delegate = self
                     
            return cell
            
            }     
        
}

 func didSelect() {
        
   // how can i add indexpath.row  as showen below it wont allow me to do it?

    if indexpath.row == 0 {
        
         performSegue(withIdentifier: "BicyclesOptionseg", sender: self)
                
        
    } else {
   
    if indexpath.row == 2 {

   performSegue(withIdentifier: "CarsOptionseg", sender: self)
  }

 }

要解决这个问题,您只需将所需的参数添加到您的 delegate 方法中即可。看起来你不需要整个 IndexPath,只需要一行,所以这样做:

protocol SegueSelectionDelegate: class {
    func didSelect(index: Int) // Can also change the parameter to IndexPath if you want but not necessary
}

然后在collectionView的didSelect方法中:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    delegate?.didSelect(index: indexPath.row)
}

最后在 MainTableViewController 中:

class MainTableViewController: UIViewController, MainTableViewCellDelegate , SegueSelectionDelegate {
    func didSelect(index: Int) {
        // Now you have access to index and can use it
        if index == 0 {
            performSegue(withIdentifier: "BicyclesOptionseg", sender: self)
        }
    }
}