【问题标题】:Perform segue from CollectionCell inside tableview从 tableview 内的 Collection Cell 执行 segue
【发布时间】:2021-07-19 05:42:39
【问题描述】:

如何从 tableview 中的 collectioncell 执行 segue?我在tableview 中创建了一个collectionview,我想从collectioncell 到另一个veiwcontroller 执行segue。每个单元格都有自己的转场。例如,“ if indexpath.row = 0 执行 segue 到标识符“A” if indexpath.row == 1 perferom segue 到标识符“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)
  }

 }

【问题讨论】:

    标签: ios swift uitableview uicollectionview protocols


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      相关资源
      最近更新 更多