首页 > 解决方案 > TableViewCell 数据传输但数据迟到 iOS

问题描述

我正在实现一个函数,该函数在单击数据表单元格时将单元格的标题发送到下一个控制器的 JSON 文件名。

数据顺利通过,数据却一一迟到。如果单击第一个单元格,数据不会消失,如果单击第二个单元格,则会传输第一个单元格的内容。

我在哪里可以一一调整迟到的数据?有任何想法吗?

vc1

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        guard let nextViewController: SecondViewController = segue.destination as? SecondViewController else {
            return
        }
        guard let cell: UITableViewCell = sender as? UITableViewCell else {
            return
        }
        nextViewController.title = cell.textLabel?.text
        nextViewController.secondAssetName = jsonName
    }


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let country: Countries = countries[indexPath.row]
        jsonName = country.asset_name
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return countries.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath)
        let country: Countries = countries[indexPath.row]
        cell.imageView?.image = UIImage(named: "flag_" + country.asset_name)
        cell.textLabel?.text = country.korean_name
        return cell
    }
    
    // Data Transfer
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        guard let nextViewController: SecondViewController = segue.destination as? SecondViewController else {
            return
        }
        guard let cell: UITableViewCell = sender as? UITableViewCell else {
            return
        }
        nextViewController.title = cell.textLabel?.text
        nextViewController.secondAssetName = jsonName
    }
}

vc2

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var weathers = [Weather]()
    var secondAssetName: String?
    
    @IBOutlet weak var tableView: UITableView!
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let jsonDecoder = JSONDecoder()
        guard let dataAsset = NSDataAsset(name: secondAssetName ?? "") else {
            return
        }
        do {
            weathers = try jsonDecoder.decode([Weather].self, from: dataAsset.data)
        } catch {
            print(error.localizedDescription)
        }
        tableView.reloadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
    }
    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return weathers.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: CustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
        let weather: Weather = weathers[indexPath.row]
        
        switch weather.state {
        case 10:
            cell.cellImageView?.image = UIImage(named: "sunny.png")
        case 11:
            cell.cellImageView?.image = UIImage(named: "cloudy.png")
        case 12:
            cell.cellImageView?.image = UIImage(named: "rainy.png")
        case 13:
            cell.cellImageView?.image = UIImage(named: "snowy.png")
        default:
            return cell
        }
        cell.cityNameLabel.text = weather.city_name
        cell.temperatureLabel.text = String(weather.celsius)
        cell.rainfallProbabilityLabel.text = String(weather.rainfall_probability)
        return cell
    }
}

标签: ios

解决方案


在代码中添加断点。你应该看看问题出在哪里。prepare(for: sender:)之前被调用tableView(_: didSelectRowAt:),所以你第一次点击一个单元格时,在 .jsonName期间为 nil prepare,然后在didSelect. 第二次点击它,jsonName具有第一次点击的值,然后更新。

将所有逻辑放在一个地方。删除该didSelect方法,并prepare像这样更新:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        guard let nextViewController: SecondViewController = segue.destination as? SecondViewController else {
            return
        }
        guard let cell: UITableViewCell = sender as? UITableViewCell else {
            return
        }
        guard let indexPath = tableView.indexPath(for: cell) else {
            return
        }

        let country: Countries = countries[indexPath.row]
        nextViewController.title = country.korean_name
        nextViewController.secondAssetName = country.asset_name
    }

推荐阅读