我有此代码:
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:
在单击“ festivalBtn1”按钮后,如何显示“被单击的产品ID:......”?
我该怎么做?
答案 0 :(得分:0)
您可以使用带有标签的概念来识别被按下的对象,
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)
}
答案 1 :(得分:0)
您可以按照以下方式为按钮设置标签:
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: ")
}
答案 2 :(得分:0)
您需要为自己喜欢的按钮设置标签:
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)
}