ios - 如何删除 UITableView 中的行并更新 UITableViewCell 中的标签

标签 ios swift uitableview uitabbarcontroller

我遇到了一种情况,我使用函数 commiteditingStyle 从 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()

        }
    }
}

这是一张照片:

enter image description here

感谢您抽出时间阅读本文,祝您有美好的一天!

最佳答案

只需在重新加载表格 View 之前调用“fetchSelectedProducts()”函数即可。 这样你就得到了更新后的总和。

因此,从购物车中删除产品的功能将如下所示,

//从购物车中删除产品的函数

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()

    }
}

关于ios - 如何删除 UITableView 中的行并更新 UITableViewCell 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51151039/

相关文章:

iphone - iOS——有没有类似 CGContextSetLineDash 的路径?

ios - 使用 iOS4.3/calendars 同步到 Google 日历的 EventKit 中未正确创建事件警报

swift - 来自 URL html source Swift 的奇怪输出

ios - 如何访问自定义单元格文本字段值 Swift 3.0

ios - 如何避免与共享项目重复代码文件?

ios - 来自 AppDelegate 的 xcode 5 访问函数

ios - ErrorType - 仅限 iOS9?

ios - 断点不适用于 Xcode 上的特定类

ios - 如何基于父 View 组件重新定位tableView

ios - 带分页的UITableView,每个cell是一个UIScrollView