1

I have this code:

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell10", for: indexPath) as! MainViewCollectionViewCell

        let objProduct = productsObjectArray[indexPath.item]

        cell.titleLabel.text = objProduct.name
        let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let imageFileName = objProduct.code
        let fullImagePath = documentsDir.appendingPathComponent("GET_PHOTO").path + "/" + imageFileName! + ".jpg"
        cell.imageView.image = UIImage(contentsOfFile:   fullImagePath)

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(MainViewControler.rightSwiped))
        swipeRight.direction = UISwipeGestureRecognizerDirection.right
        cell.addGestureRecognizer(swipeRight)

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(MainViewControler.leftSwiped))
        swipeLeft.direction = UISwipeGestureRecognizerDirection.left
        cell.addGestureRecognizer(swipeLeft)

        cell.favoriteBtn1.tag = indexPath.row
        cell.favoriteBtn1.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)

        return cell
    }

    @objc func didTapButton(_ sender: UIButton) {
        let objProduct = productsObjectArray[indexPath.item]
        print(objProduct.id) 
        print(objProduct.code) 
    }


   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let productObject = productsObjectArray[indexPath.item]
        showSubViewInContainerViewProductCard(view: "ProductDetailView", object: [productObject], productsFilter: 0, marketProductsFilter: 0, menuFilter: 0, preparationFilter: 0, glutenFilter: 0,  backFromProductView: false)
    }

This is my StoryBoard:

enter image description here

How can I display in "clicked product id: ......" after clicking button favoriteBtn1 which product code was clicked?

How can I do this?

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
traff
  • 135
  • 5

3 Answers3

1

you can use with tag concept for identify which object you are pressed,

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

    let productCode = productsObjectArray[indexPath.item].code
    cell.favoriteBtn1.tag =  indexPath.item
    cell.favoriteBtn1.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
}

and your button action

@objc func didTapButton(_ sender: UIButton) {
    let productCode = productsObjectArray[sender.tag].code
    print("clicked product id: ", productCode)
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • is posible to change this to: cell.favoriteBtn1.addTarget(self, action: #selector(didTapButton(_:, favoriteType: 1)), for: .touchUpInside) and @objc func didTapButton(_ sender: UIButton, favoriteType: Int) { let productCode = productsObjectArray[sender.tag].code print("clicked product id: \(productCode)") } ? Now I have error: Expected expression in list of expressions – traff Jul 09 '18 at 10:15
  • @traff- no you can't, see this for e.g https://stackoverflow.com/questions/20280627/for-uibutton-how-to-pass-multiple-arguments-through-selector – Anbu.Karthik Jul 09 '18 at 10:18
  • I do not understand object c :( Could I ask for an example in swift? – traff Jul 09 '18 at 10:23
  • @traff - see this for alternate : https://stackoverflow.com/questions/33498064/pass-multiple-parameters-to-addtarget – Anbu.Karthik Jul 09 '18 at 10:30
  • @traff, If you want to pass multiple data then you can try this. `cell.btnLike.accessibilityHint = ""` `cell.btnLike.accessibilityIdentifier = ""` – Kuldeep Jul 09 '18 at 10:32
0

You can set a tag for your button like below way:

cell.favoriteBtn1.tag = indexPath.row

Inside button's action you can get the indexPath of that cell on which the button is pressed and hence you can get your productid like below:

@objc func didTapButton(_ sender: UIButton) {
    //Here you can get your id
    let productId = productsObjectArray[sender.tag].id
    print("clicked product id: ") 
}
iPeter
  • 1,330
  • 10
  • 26
0

Your need to set the tag for your favourite button:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell10", for: indexPath) as! MainViewCollectionViewCell

        cell.favoriteBtn1.tag = indexpath.row

        return cell
    }

After that you can fetch product ID like this:

@objc func didTapButton(_ sender: UIButton) {
        let productObject = productsObjectArray[sender.tag]
        print("clicked product id: ", productObject.ID) 
    }
Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28