Swift
Swift

Reputation: 1182

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

I am using collectionview in tableview cell,

i need to pass selected collectionview cells value to next viewcontroller, how?

code: here is the code for tableview and 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)
}

}

here if i use didSelectItemAt for collectionview to send its selected cell value to next view controller

error:

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

if i give button action in main class then able to push but value is not going

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

here how to pass selected collectionview cells value to SearchResultVC please do help

EDIT

according to below answer i have added: still didSelectItemAt not called, why plz do help

  class CategoryNewTableCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{

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

//

Upvotes: 0

Views: 488

Answers (2)

Tarun Tyagi
Tarun Tyagi

Reputation: 10112

  1. Declare a callback in the tableViewCell subclass.
class CategoryNewTableCell: UITableViewCell {
    var onSelectSubcategory: ((_ subcategoryID: String) -> Void)?
}
  1. Assign this callback in your cellForRow like this.
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. Invoke this callback from collectionView didSelectItem like this.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let subcategoryID = sub_categories?[indexPath.item].slug ?? ""
    self.onSelectSubcategory?(subcategoryID)
}

Upvotes: 1

Dhara Patel
Dhara Patel

Reputation: 506

You can use protocol to send data to your nextviewcontroller.

protocol CategorySelectionDelegate {
    func get(category: Sub_categories)
}

Declare the delegate in your CategoryNewTableCell and use it in didSelectItemAt method of your collectionviewcell like below:

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

Adopt the protocol in the receiving 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)
    }
}

Upvotes: 3

Related Questions