I have addToCartButton
in UICollectionViewCell
. What I have to do is to grab the details of the product of the specific cell when the user tapped the button and list it to the another UIViewController
. How can It be done in easy and reliable way?
Here is what I have done :
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(){
}
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]
}
Try this
you can define protocol on collectionViewCell
and fire it when the addToCard
button hits on each cell, and in your viewController
implement of delegate:
protocol CollectionViewCellDelegate: class {
func addtoCard(_ cell: TrendingProductsCVCell)
}
class TrendingProductsCVCell: UITableViewCell {
weak var delegate: CollectionViewCellDelegate?
// ... the rest of your code
@IBAction func addToCardButtonClicked(_ sender: UIButton) { //action of ***addToCard***
delegate?.addToCard(self)
}
}
class viewController: CollectionViewCellDelegate { //and what you want to implement
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = trendingProductCV.dequeueReusableCell(withReuseIdentifier: "TrendingProductsCVCell", for: indexPath) as? TrendingProductsCVCell
cell.delegate = self
// ... the rest of your code
return cell
}
func addToCard(cell: TrendingProductsCVCell) {
//do what you want with your cell and its content
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.