使用UICollectionViewCell中的按钮,如何获取特定Cell的内容详细信息?

时间:2019-01-19 06:33:52

标签: ios swift uicollectionview

我在addToCartButton中有UICollectionViewCell。我要做的是,当用户点击按钮时,获取特定单元格产品的详细信息,并将其列出到另一个UIViewController中。如何以简单可靠的方式完成该任务?

enter image description here

这是我所做的:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = trendingProductCV.dequeueReusableCell(withReuseIdentifier: "TrendingProductsCVCell", for: indexPath) as? TrendingProductsCVCell
    cell?.trendingProductImage.downloadImages(url: trendingProductsDataArray[indexPath.row].images![0].source!)
    cell?.trendingProducttitle.text = trendingProductsDataArray[indexPath.row].title
    cell?.trendingProductSellingPrice.text =  trendingProductsDataArray[indexPath.row].salePrice
    cell?.struckTest(unstruckedText: trendingProductsDataArray[indexPath.row].regularPrice!)
    cell?.trendingAddToCartBtn.addTarget(self, action: #selector(addToCartBtnTapped), for: .touchUpInside)
    return cell!
}

@objc
func addToCartBtnTapped(){

}

2 个答案:

答案 0 :(得分:0)

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

    let productData = trendingProductsDataArray[indexPath.row]
    let cell = trendingProductCV.dequeueReusableCell(withReuseIdentifier: "TrendingProductsCVCell", for: indexPath) as? TrendingProductsCVCell
    cell?.trendingProductImage.downloadImages(url: productData.images![0].source!)
    cell?.trendingProducttitle.text = productData.title
    cell?.trendingProductSellingPrice.text =  productData.salePrice
    cell?.struckTest(unstruckedText: productData.regularPrice!)
    cell?.trendingAddToCartBtn.addTarget(self, action: #selector(addToCartBtnTapped(sender:)), for:.touchUpInside)

    return cell!
}

@objc func addToCartBtnTapped(sender:UIButton) {

    let buttonPosition:CGPoint = sender.convert(CGPoint.zero, to:self.tblUsers)
    let indexPath = self.tblUsers.indexPathForRow(at: buttonPosition)
    let productData = trendingProductsDataArray[indexPath.row]
}

尝试一下

答案 1 :(得分:0)

您可以在collectionViewCell上定义 protocol ,并在每个单元格上的{{1} }委托的实现:

addToCard