【问题标题】:How get product code when pressed button on uicollectionViewCell?按下uicollectionViewCell上的按钮时如何获取产品代码?
【发布时间】:2018-07-09 10:05:32
【问题描述】:

我有这个代码:

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

这是我的故事板:

点击按钮 favoriteBtn1 后如何在“clicked product id: ......”中显示点击了哪个产品代码?

我该怎么做?

【问题讨论】:

  • 请查看您更新的问题。

标签: ios swift uicollectionview


【解决方案1】:

您可以使用 tag 概念来识别您按下的是哪个对象,

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

以及您的按钮操作

@objc func didTapButton(_ sender: UIButton) {
    let productCode = productsObjectArray[sender.tag].code
    print("clicked product id: ", productCode)
}

【讨论】:

  • 可以将其更改为: cell.favoriteBtn1.addTarget(self, action: #selector(didTapButton(:, favoriteType: 1)), for: .touchUpInside) 和 @objc func didTapButton( sender: UIButton, favoriteType: Int) { let productCode = productsObjectArray[sender.tag].code print("clicked product id: (productCode)") } ?现在我有错误:表达式列表中的预期表达式
  • @traff- 不,你不能,看到这个例如stackoverflow.com/questions/20280627/…
  • 我不理解对象 c :( 我可以快速询问一个例子吗?
  • @traff - 看到这个备用:stackoverflow.com/questions/33498064/…
  • @traff,如果你想传递多个数据,那么你可以试试这个。 cell.btnLike.accessibilityHint = ""cell.btnLike.accessibilityIdentifier = ""
【解决方案2】:

你可以像下面这样为你的按钮设置一个标签:

cell.favoriteBtn1.tag = indexPath.row

button's 操作中,您可以获得按下按钮的那个单元格的 indexPath,因此您可以获得如下所示的 productid:

@objc func didTapButton(_ sender: UIButton) {
    //Here you can get your id
    let productId = productsObjectArray[sender.tag].id
    print("clicked product id: ") 
}

【讨论】:

  • 它在indexpath.rowindexpath.item
【解决方案3】:

您需要为您喜欢的按钮设置标签:

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
    }

之后,您可以像这样获取产品 ID:

@objc func didTapButton(_ sender: UIButton) {
        let productObject = productsObjectArray[sender.tag]
        print("clicked product id: ", productObject.ID) 
    }

【讨论】:

  • 它在indexpath.rowindexpath.item
  • 这是 indexpath.row
  • indexPath.row 和 indexPath.item 在编译器方面是相同的。但是 indexPath.item 通常用于集合视图中,只是作为一种编程风格
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多