Возникли проблемы с UIStepper на .valueChange
.Все хорошо, но не на каждом клике, значение изменится после двух кликов.
Какую часть я здесь делаю неправильно?
@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)
}
}
}