单击按钮时如何增加和减少值?

时间:2019-07-12 00:56:46

标签: swift

我试图单击增加和减少按钮并更改我的值,但是我单击增加按钮而不是增加我的值,但是我单击减少按钮,但我的值不在api响应中。

这是我的代码:

func  dataMyCartListAPI() {

    let id = UserInfo.loggedInUserId
    print(id ?? "")

    let param: [String: Any] = ["user_id": id ?? "", "token":UserInfo.loggedInUser?.accessToken ?? ""]
    print(param)
    if self.pageNo == 0 {
        commonClass.sharedInstance.startLoading()
    }

    webserviceClass.sharedInstance.restShopServiceHandler(methodType: 1, urlString: kShopGetViewCartUrl, parameters: param as [String : AnyObject]) { (success, response) in

    commonClass.sharedInstance.stopLoading()
    let status = response["status"] as? Int
    if status == 0 {
        commonClass.sharedInstance.showErrorMessage("You have no order data")
    }else{
        commonClass.sharedInstance.showSuccessMessage( "You have  order data")
    }

    guard let dataArray = response["data"]  as? [[String: AnyObject]] else { return}
    print(dataArray)
    let data = MyCartData()
    self.cartDataArray = data.getCartList(dataArray: dataArray)

    for item in dataArray{
        if let price = item["total_price"] as? Int{
            self.totalPrice += price.toDouble
            self.totalammount.text = "\( self.totalPrice)"

        }

    }

    self.tableView.reloadData()

}

//MARK: - Increaes CART

func AddToIncreaseProduct(storeId:String,productId:Int,color:String,size:String,qty:Int) {

    let saveDeviceId = UserDefaults.standard.object(forKey: "deviceId") as? String ?? ""
    printD(saveDeviceId)

    let id = UserInfo.loggedInUserId
    print(id ?? "")
    let param: [String: Any] = ["product_id": productId,"user_id": id ?? "","store_id":storeId,"color": color ,"size": size ,"device_id":saveDeviceId,"qty":qty, "token":UserInfo.loggedInUser?.accessToken ?? ""]
    print(param)
    if self.pageNo == 0 {
        commonClass.sharedInstance.startLoading()
    }

    webserviceClass.sharedInstance.restShopServiceHandler(methodType: 0, urlString: kShopGetAddToCartUrl, parameters: param as [String: AnyObject]) { (success, response) in
        commonClass.sharedInstance.stopLoading()
        let status = response["status"] as? Int
        if status == 0 {
            commonClass.sharedInstance.showSuccessMessage("Sneaker already added to your cart.")
        }else{
            commonClass.sharedInstance.showSuccessMessage( "Product added in cart succesfully.")
        }
        if !success {
            commonClass.sharedInstance.showErrorMessage(response["msg"] as? String ?? "Server error. Please try again later.")

            return
        }

        self.tableView.reloadData()
    }
}

//MARK: - Decrease CART

func AddToDecreaseProduct(storeId:String,productId:Int,color:String,size:String,qty:Int) {

    let saveDeviceId = UserDefaults.standard.object(forKey: "deviceId") as? String ?? ""
    printD(saveDeviceId)
    let id = UserInfo.loggedInUserId
    print(id ?? "")
    let param: [String: Any] = ["product_id": productId,"user_id": id ?? "","store_id":storeId,"color": color ,"size": size ,"device_id":saveDeviceId,"qty":qty, "token":UserInfo.loggedInUser?.accessToken ?? ""]
    print(param)
    if self.pageNo == 0 {
        commonClass.sharedInstance.startLoading()
    }

    webserviceClass.sharedInstance.restShopServiceHandler(methodType: 0, urlString: kShopGetAddToCartUrl, parameters: param as [String: AnyObject]) { (success, response) in
        commonClass.sharedInstance.stopLoading()

        let status = response["status"] as? Int
        if status == 0 {
            commonClass.sharedInstance.showSuccessMessage("Sneaker already added to your cart.")
        }else{
            commonClass.sharedInstance.showSuccessMessage( "Product added in cart succesfully.")
        }
        if !success {
            commonClass.sharedInstance.showErrorMessage(response["msg"] as? String ?? "Server error. Please try again later.")
            return
        }
        self.tableView.reloadData()
    }
}

/// MARK: - Button Action

@IBAction func increaseButtonAction(_ sender: UIButton) {

    cartDataArray[sender.tag].qty  = (cartDataArray[sender.tag].qty  + 1)
    let indexPath = IndexPath(row: sender.tag, section: 0)
    let cell = tableView.cellForRow(at: indexPath)as! MyCartCell

    cell.totalCountLabel.text = "\(cartDataArray[sender.tag].qty )"
    AddToIncreaseProduct(storeId: cartDataArray[sender.tag].option.storeId, productId: cartDataArray[sender.tag].product_id, color: cartDataArray[sender.tag].option.color, size: cartDataArray[sender.tag].option.size, qty: cartDataArray[sender.tag].qty)

}

@IBAction func decreaseButtonAction(_ sender: UIButton) {

    cartDataArray[sender.tag].qty  = (cartDataArray[sender.tag].qty - 1)
    let indexPath = IndexPath(row: sender.tag, section: 0)
    let cell = tableView.cellForRow(at: indexPath)as! MyCartCell
    cell.totalCountLabel.text = "\(cartDataArray[sender.tag].qty )"

    AddToDecreaseProduct(storeId: cartDataArray[sender.tag].option.storeId, productId: cartDataArray[sender.tag].product_id, color: cartDataArray[sender.tag].option.color, size: cartDataArray[sender.tag].option.size, qty: cartDataArray[sender.tag].qty)
}

我不明白自己在代码中做错了什么。有人可以帮我吗?

编辑:代码已被编辑,我必须添加一些行,以便将代码发布为一个块。

1 个答案:

答案 0 :(得分:0)

我不知道您的webServiceClass的样子,但是在您的增加和减少方法中,您都将0作为参数传递。方法类型参数最有可能决定增加还是减少数量?

webserviceClass.sharedInstance.restShopServiceHandler(methodType: 0, urlString: kShopGetAddToCartUrl, parameters: param as [String: AnyObject]) { (success, response) in

这可能是问题吗?请检查methodType参数。