swift 如何将 collectionview 单元格索引值传递给 nextviewcontroller(使用推送导航)

How to pass collectionview cells index value to nextviewcontroller(using push navigation) in swift

我在表格视图单元格中使用集合视图,

我需要将选定的 collectionview 单元格值传递给下一个 viewcontroller,如何?

代码:这里是tableview和collectionview cell的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryNewTableCell", for: indexPath) as! CategoryNewTableCell

        let indexData = self.activeCategories?[indexPath.row]
        cell.selectionStyle = .none
        cell.catNameLbl.text = indexData?.details?.first?.title

        cell.subCategories = indexData?.sub_categories
        cell.clcSeller.reloadData()
 }



class CategoryNewTableCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{

@IBOutlet weak var clcSeller: UICollectionView!

public var subCategories : Array<Sub_categories>?

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return subCategories?.count ?? 0
 }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SubCatCollectionCell", for: indexPath) as! SubCatCollectionCell

    let subCategory = self.subCategories?[indexPath.item]
    cell.lblTitle.text = langType == .en ? subCategory?.details?.first?.title : subCategory?.details?[1].title

    return cell
 }

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let vc = StoryBoard.main.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultVC
    vc.subCatId = sub_categories?[indexPath.row].slug ?? ""
    self.navigationController?.pushViewController(vc, animated: true)
}

}

这里如果我使用 didSelectItemAt collectionview 将其选定的单元格值发送到下一个视图控制器

错误:

Value of type 'CategoryNewTableCell' has no member 'navigationController'

如果我在 main class 中给出按钮操作,则可以按下但值不会变化

class CategoryNewVC: UIViewController {

@IBAction func didselectcollectionviewBTn(_ sender: UIButton) {

    let vc = StoryBoard.main.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultVC

    vc.subCatId = //here how to pass value

    self.navigationController?.pushViewController(vc, animated: true)
 }
}

此处如何将选定的 collectionview 单元格值传递给 SearchResultVC 请帮忙

编辑

根据下面的回答我添加了:仍然didSelectItemAt没有被调用,为什么请帮助

  class CategoryNewTableCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{

override func awakeFromNib() {
    super.awakeFromNib()
    
    self.clcSeller.delegate = self
    self.clcSeller.dataSource = self

//

您可以使用协议将数据发送到您的 nextviewcontroller。

protocol CategorySelectionDelegate {
    func get(category: Sub_categories)
}

在您的 CategoryNewTableCell 中声明委托,并在您的 collectionviewcell 的 didSelectItemAt 方法中使用它,如下所示:

class CategoryNewTableCell:
    UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{
    
    @IBOutlet weak var clcSeller: UICollectionView!
    
    public var subCategories : Array<Sub_categories>?
    
    var delegate: CategorySelectionDelegate?
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return subCategories?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SubCatCollectionCell", for: indexPath) as! SubCatCollectionCell
        
        let subCategory = self.subCategories?[indexPath.item]
        cell.lblTitle.text = langType == .en ? subCategory?.details?.first?.title : subCategory?.details?[1].title
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        delegate?.get(category: sub_categories?[indexPath.row])
    }
}

接收中采用协议class

class CategoryNewVC: UIViewController, CategorySelectionDelegate {
    func get(category: Sub_categories) {
        let vc = StoryBoard.main.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultVC
        vc.subCatId = category.slug ?? ""
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
  1. 在 tableViewCell 子类中声明回调。
class CategoryNewTableCell: UITableViewCell {
    var onSelectSubcategory: ((_ subcategoryID: String) -> Void)?
}
  1. 像这样在您的 cellForRow 中分配此回调。
cell.subCategories = indexData?.sub_categories
cell.onSelectSubcategory = { [weak self] (subcategoryID) in
    let vc = StoryBoard.main.instantiateViewController(withIdentifier: "SearchResultVC") as! SearchResultVC
    vc.subCatId = subcategoryID
    self?.navigationController?.pushViewController(vc, animated: true)
}
  1. 像这样从 collectionView didSelectItem 调用此回调。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let subcategoryID = sub_categories?[indexPath.item].slug ?? ""
    self.onSelectSubcategory?(subcategoryID)
}