ios - TableView 不更新

标签 ios swift4.2 xcode9.4

我正在尝试为餐厅制作一个点餐应用程序,当我第一次将项目添加到购物车时,表格 View 加载了我制作的单例数组。但是当我返回菜单并选择另一个项目时,数组已更新但购物车的表格 View 没有,我正在使用 tabbarcontroller。尝试在不同的地方使用 tableview.reloadData() 仍然没有出现添加到数组的新数据

class CartVC: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var priceLbl: UILabel!
    @IBOutlet weak var tableView: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.reloadData()
        // Do any additional setup after loading the view.
        tableView.delegate = self
        tableView.dataSource = self
        tableView.reloadData()

        updateView()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return DataService.instance.cartItems.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell") as? CartCell{
            let item = DataService.instance.cartItems[indexPath.row]
            cell.configCell(cart: item)
            return cell
        } else {
            return UITableViewCell()
        }
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            DataService.instance.cartItems.remove(at: indexPath.row)

            tableView.beginUpdates()
            tableView.deleteRows(at: [indexPath], with: .automatic)
            updateView()
            tableView.endUpdates()
        }
    }

    func updateView(){
        var sum = 0
        for item in DataService.instance.cartItems{
            sum += item.itemPrice * item.quantity
            print(item.itemPrice)
        }
        priceLbl.text = "$\(sum)"
    }

    @IBAction func orderPressed(_ sender: Any) {
        // upload order to firebase, clear cart and move to orderVC
    }


}

最佳答案

每次 viewDidLoad 未调用时,在标签栏中,您需要重新加载 viewWillAppearviewDidAppear 中的数据。这是引用。

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.delegate = self
        tableView.dataSource = self

        updateView()
    }
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        tableView.reloadData()

    }

关于ios - TableView 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54018783/

相关文章:

javascript - iPhone 应用程序或 Mobile Safari 的动画 PNG 文件的替代方案,目标是模拟短视频剪辑的即时播放

ios - 无法加载从带标识符的 bundle 中的 nib 引用的图像(再次)

ios - 如何使用 XIB 在 UITableView 中使用静态单元格的数量

swift - iOS RxSwift - 使用 RxTest 和 TestScheduler 进行测试不会结束/终止测试用例

ios - iOS上的Twitter-如何授予发布推文的权限?

Swift 按案例名称对枚举进行排序

ios - Storyboard困惑 : UITextView and UIView not visible, UITextFields 不可编辑?

ios11 - Xcode 9.4 : unexpected service error: The Xcode build system has crashed

ios - xCode 9.4 和 iOS 11 每次调用 ViewDidLoad() 方法

ios - 如何使用 NSUserDefaults 设置 bool 值。预期声明错误