首页 > 解决方案 > 从 Tableview 中的 CollectionCell 执行 segue

问题描述

如何从tableview中的collectioncell执行segue?我在tableview 中创建了一个collectionview,我想从collectioncell 到另一个veiwcontroller 执行segue。每个单元格都有自己的转场。例如,“ if indexpath.row = 0 如果 indexpath.row == 1 perferom segue 到标识符“B”,则执行到标识符“A”。谢谢,这是我的代码。

   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)
  }

 }

标签: iosswiftuitableviewuicollectionviewprotocols

解决方案


要解决此问题,您只需将所需的参数添加到您的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)
        }
    }
}

推荐阅读