When user add the product to cart.The selected product must show the increment and decrement button. and others product should only show the add button. You can check this image
The problem is that when I comes back to this screen again it should show the quantity label till The product is in cart. as I have no property in backend that stores the boolean value that product is in cart or not ? Can anybody tell the same thing how to do this in frontend ? so that only items in cart shows the quantity label in product TableView.
Here is my code
Product TableView
func getCartDetails()
{
ApiCaller().getData(url: get_cart_url, resultType: CartResult.self) { (results) in
self.iddd = results.result!.id
self.c = results.result!
guard let resultArray = results.result?.cartItems as? [CartItem] else {return }
UserDefaults.standard.set(results.result?.cartItems.count, forKey: "totalItemsInCart")
for cart in resultArray
{
self.cAr.append(cart)
}
}
}
func addItem(prodId:String,qty:Int,cartId:String)
{
let request = cartProducts(products: [addCartProducts(prodId:prodId, q: qty + 1)])
do
{
let encodedResult = try JSONEncoder().encode(request)
ApiCaller().postData(url: update_cart_url+cartId+"/update", requestBody: encodedResult, requestMethod: "PUT", resultType: UpdateCartResults.self) { (results) in
print(results.statusCode)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}catch {
print(error)
return
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ProductsTVCell
cell.generateCells(products: productArray[indexPath.row])
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: rupee + "\(productArray[indexPath.row].mrpAfterDiscount)")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: NSMakeRange(0, attributeString.length))
cell.secondPriceLbl.attributedText = attributeString
cell.addBtnLbl.tag = indexPath.row
let productAtIndex = self.productArray[indexPath.row]
if cAr.contains(where: { (item) -> Bool in
productAtIndex.id == item.id
}){
// product is in cart, show increment/decrement button
cell.addBtnLbl.isHidden = true
} else {
// product not in cart, show add button
cell.addBtnLbl.isHidden = false
}
cell.callBackForAddButtonHidden = {
cell.addBtnLbl.isHidden = true
guard self.c != nil else {
print("Creating cart for the first time")
self.createCart(prodId: self.productArray[indexPath.row].id, qty: 1)
return
}
self.addItem(prodId: self.productArray[indexPath.row].id, qty: 0, cartId: self.c!.id)
}
cell.selectionStyle = .none
return cell
}
Here is my product TableView cell I tried using comparing cartDetails and productDetails but it did not worked so i commented it that.
func generateCells(products:Product)
{
self.productNameLbl.text = products.name
self.productDescriptionLbl.text = products.additionInfo
self.productPriceLbl.text = rupee + String(products.productMRP)
if products.discountPercentage != 0
{
self.discountLbl.text = "\(products.discountPercentage)% Off"
} else {
self.discountLbl.isHidden = true
}
//
// if products.id == cartDetails.id
// {
// addBtnLbl.isHidden = true
// self.quantityLbl.text = "\(cartDetails.qty)"
// } else {
// addBtnLbl.isHidden = false
// }
self.productImgView.sd_setImage(with: URL(string: products.imgURL)) { (image, error, cache, url) in
}
}

Assuming that your card is an array of
Product, you can check in yourcellForRowAt indexPathwhether the item at current index is present in your cart and show/hide buttons accordingly.