每次点击都无法更改UIStepper值

时间:2018-05-31 21:25:16

标签: swift uicollectionviewcell uistepper

.valueChange上的UIStepper出现问题。 每件事情都很好但不是每次点击都会有效,两次点击后价值会发生变化。

我在这里做错了什么?

    @IBAction func stepperAction(sender: Stepper)  {
        ApiServiceCart.sharedInstance.UpdateCart(rowId: sender.tag, value: sender.value) { (success, carts) in
            if success == true{
                let indexPath = IndexPath(row: sender.indexPath!, section: 0)
                let cell = self.collectionView?.cellForItem(at: indexPath) as! CartCell
                cell.cart = carts[indexPath.item]
                let quantity = cell.cart?.Quantity?.stringValue
                cell.countLabelText.text = quantity
            }
            else{
                self.displayMessage(Title:MessageTitle.Error.rawValue, Message:Message.Error.rawValue)
            }
        }
    }


    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CartCell
        cell.cart = carts?[indexPath.item]

        let cartId = Int(cell.hiddenLabel.text!            
        let itemQuantity = Double(cell.countLabelText.text!)
        cell.stepperElement.tag = cartId!
        cell.stepperElement.value = itemQuantity!
        cell.stepperElement.indexPath = indexPath.row
        cell.stepperElement.addTarget(self, action: #selector(stepperAction(sender:)), for: .valueChanged)

        return cell
    }

这是ApiServiceCart

  func UpdateCart(rowId: Int,value: Double,completion: @escaping (Bool,[Cart]) -> ()){
        let headers: HTTPHeaders = [:]
        let urlString = "\(BaseUrl.Protina.rawValue)/ApiShoppingCart/UpdateCart"
        let parameters: Parameters = ["itemquantity\(rowId)": value, "Content-Type": "application/form-data"]
        retrieveFromUrl(urlString: urlString,parameters: parameters,headers: headers,encoding: URLEncoding.httpBody ,completion: completion)
    }

    func retrieveFromUrl(urlString: String,parameters: Parameters,headers: HTTPHeaders,encoding: ParameterEncoding ,completion: @escaping (Bool,[Cart])->()) {
        var myCart = [Cart]()
        Alamofire.request(urlString, method: .post, parameters: parameters,encoding: encoding, headers: headers).responseJSON { response in

            switch response.result {
            case .success(let data):
                let jsonData = JSON(data)

                if jsonData.count > 0
                {
                    for (_, value) in jsonData
                    {
                        let cart = Cart()
                        cart.Id = value["Id"].int
                        cart.ProductName = value["ProductName"].string
                        cart.ProductId = value["ProductId"].int
                        cart.Quantity = value["Quantity"].int
                        cart.UnitPrice = value["UnitPrice"].string

                        myCart.append(cart)
                    }
                }

                DispatchQueue.main.async {
                    completion(true,myCart)
                }

            case .failure(let error):
                print(error)
                completion(false,myCart)
            }
        }
    }

0 个答案:

没有答案