swift2 当产品在购物车中时,显示数量标签(Swift 5)

osh3o9ms  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(203)

当用户将产品添加到购物车时。所选产品必须显示增量和减量按钮。而其他产品应仅显示添加按钮。您可以检查此图像

问题是,当我再次回到这个屏幕时,它应该显示数量标签,直到产品在购物车中。因为我在后端没有属性来存储产品是否在购物车中的布尔值?有人能告诉同样的事情如何在前端中完成吗?以便只有购物车中的项目在产品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
    }

这是我的产品TableView单元格,我尝试使用它来比较cartDetails和productDetails,但它不起作用,所以我对它进行了注解。

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

        }

    }
a6b3iqyw

a6b3iqyw1#

假设您的卡片是一个Product数组,您可以在cellForRowAt indexPath中检查当前索引处的商品是否存在于您的购物车中,并相应地显示/隐藏按钮。

let productAtIndex = self.productArray[indexPath.row]

    if cart.contains(where: { (item) -> Bool in
        productAtIndex.id == item.id
    }){
        // product is in cart, show increment/decrement button
    } else {
        // product not in cart, show add button
    }

相关问题