在触发表视图委托之前迅速用函数更新局部变量

问题描述

嗨,我有以下代码,我在其中使用回调函数获取模型中的某些数据(我在其中进行API调用获取一些数据)。数据将传递到此viewController。因此,我想使用数据更新局部变量,以便可以使用在模型中检索到的数据填充tableView数据源委托。

但是似乎本地变量永远无法更新...谁能告诉我如何解决这个问题?谢谢。

class MyAutoListViewController: UIViewController {
    
    
    @IBOutlet weak var tableView: UITableView!

    
    var vehicleManager = VehicleManager()
    var newData: VehicleData?
    
    
    //var numberOfVehicle: Int = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        vehicleManager.retrieveUserVehicle()
        vehicleManager.onDataUpdate = { [weak self] (data: VehicleData) in
            
            self?.useData(data: data)
            
            print(">>>>>>>>new data: \(self?.newData)")
        }
        

        
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView() //remove empty tableView cells
        tableView.register(UINib(nibName: Constants.vehicleListCellNibName,bundle: nil),forCellReuseIdentifier: Constants.vehicleListToBeInsuredIdentifier)
        tableView.reloadData()
        
        
    }
    
    func useData(data: VehicleData) {
        let newData = data
        print(">>>newData is: \(newData)")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        tableView.reloadData()
    }
    
    
    
    
}

//MARK: - TableView extensions


extension MyAutoListViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return newData?._embedded.userVehicles.count ?? 0
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: Constants.vehicleListToBeInsuredIdentifier,for: indexPath) as! VehicleListTableViewCell
        cell.vehicleName.text = newData?._embedded.userVehicles[indexPath.row].vehicle.brand
        
        return cell
    }
    
    
}

这是我的调试输出图片

我可以打印出newData,它位于useData函数中,但是局部变量无法更新...它仍然为零。

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)