我遇到这样的情况:我使用功能commit editingStyle
从TableView中删除了一个产品,并且价格没有立即更新,因为我的标签位于TableViewCell中,而该产品已在功能CellForRowAt
中出队。因此,我需要以某种方式使价格立即更新。
这是我的代码:
class ProductsViewController: UIViewController{
var selectedProductsArray = [Product]()
var priceForSelectedProductsArray = [Float]()
// Func which will add the product into an array of products when the user press Add To Cart
func didTapAddToCart(_ cell: ProductTableViewCell) {
let indexPath = self.productsTableView.indexPath(for: cell)
addProduct(at: indexPath!)
selectedProductsArray.append(productsArray[(indexPath?.row)!]) // Append products for cart
priceForSelectedProductsArray.append(productsArray[(indexPath?.row)!].price) // Append prices for selected products
}
}
class CartViewController: UIViewController {
var productsInCartArray = [Product]()
var productPricesArray = [Float]()
var totalSum: Float?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
fetchSelectedProducts()
}
// Append the selectedProducts into productsInCartArray using the TabBarController
func fetchSelectedProducts() {
productsInCartArray = ((self.tabBarController?.viewControllers![0] as! UINavigationController).viewControllers[0] as! ProductsViewController).selectedProductsArray
productPricesArray = ((self.tabBarController?.viewControllers![0] as! UINavigationController).viewControllers[0] as! ProductsViewController).priceForSelectedProductsArray
totalSum = productPricesArray.reduce(0, +)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = cartTableView.dequeueReusableCell(withIdentifier: Constants.identifierCartTotalPriceCell, for: indexPath) as! CartTableViewCell
if let finalPrice = totalSum, finalPrice > 0 {
cell.cartTotalPriceLabel.text = String(format: Constants.floatTwoDecimals, finalPrice) + Constants.currencyPound
}
else{
cell.cartTotalPriceLabel.text = String(0.00) + Constants.currencyPound
}
return cell
}
// Function to delete a product from the cart
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// TO DO: update the price Instantly when I delete a product
((self.tabBarController?.viewControllers![0] as! UINavigationController).viewControllers[0] as! ProductsViewController).selectedProductsArray.remove(at: indexPath.row)
((self.tabBarController?.viewControllers![0] as! UINavigationController).viewControllers[0] as! ProductsViewController).priceForSelectedProductsArray.remove(at: indexPath.row)
totalSum = productPricesArray.reduce(0, +)
// here I need to access the `cartTotalPriceLabel.text` to be equal with `totalSum`
productsInCartArray.remove(at: indexPath.row)
cartTableView.deleteRows(at: [indexPath], with: .fade)
cartTableView.reloadData()
}
}
}
这是一张照片:
如果您正在阅读本文,谢谢您的时间,祝您有美好的一天!
答案 0 :(得分:0)
在重新加载表格视图之前只需调用“ fetchSelectedProducts()
”功能。
这样就可以得到更新的totalSum。
因此从购物车功能中删除产品看起来像这样,
//从购物车中删除产品的功能
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// TO DO: update the price Instantly when I delete a product
((self.tabBarController?.viewControllers![0] as! UINavigationController).viewControllers[0] as! ProductsViewController).selectedProductsArray.remove(at: indexPath.row)
((self.tabBarController?.viewControllers![0] as! UINavigationController).viewControllers[0] as! ProductsViewController).priceForSelectedProductsArray.remove(at: indexPath.row)
totalSum = productPricesArray.reduce(0, +)
// here I need to access the `cartTotalPriceLabel.text` to be equal with `totalSum`
productsInCartArray.remove(at: indexPath.row)
cartTableView.deleteRows(at: [indexPath], with: .fade)
fetchSelectedProducts()
cartTableView.reloadData()
}
}