gpt4 book ai didi

ios - 在 uicollectionViewCell 上按下按钮时如何获取产品代码?

转载 作者:行者123 更新时间:2023-11-30 11:21:18 26 4
gpt4 key购买 nike

我有这个代码:

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

这是我的 Storyboard:

enter image description here

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

我该怎么做?

最佳答案

您可以使用标签概念来识别您按下的对象,

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

关于ios - 在 uicollectionViewCell 上按下按钮时如何获取产品代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51242906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com