【问题标题】:tableview does not update表格视图不更新
【发布时间】:2019-01-03 08:35:35
【问题描述】:

我正在尝试为餐厅制作一个订购食品的应用程序,当我第一次将商品添加到购物车时,表格视图会加载我制作的单例数组。但是当我返回菜单并选择另一个项目时,数组会更新,但购物车的表格视图没有,我正在使用 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
    }


}

【问题讨论】:

  • @yourssef i in tabbar viewDidLoad 只调用了一次。请您在viewWillAppear 中检查重新加载。还要检查您的数组是否将您的新值添加到数组中,然后调用 reload?
  • 属性DataService.instance.cartItems修改后,需要重新加载表格视图。您需要以某种方式通知(可能是委托,回调关闭)您的 CartVC 您将在哪里执行 tableView.reloadData()
  • @chiragshah 我应该添加 viewwillappear 吗?在 cartVC 或 mainTabBarVC 中。是的,新值被添加到数组中
  • @youssefSULTAN 添加到 CartVC
  • @chiragshah 成功了,谢谢

标签: ios swift4.2 xcode9.4


【解决方案1】:

每次在标签栏中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()

    }

【讨论】:

    【解决方案2】:

    UITableViewDelegate 不是反应式的。当您想要更新 tableView 内容时(当您的 DataService.instance 值更新时),您必须调用 reloadData()。

    在 MVP 世界中,您的“添加”操作将向演示者触发一个事件。在更新您的 DataService 之后,演示者会向视图发送更新 tableView 的操作。

    也许你应该看看RxSwift/ReactiveCocoa,它提供了自动将DataService.instance 数组绑定到UITableView 中呈现的单元格的API。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多