ios - 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
}
}
解决方案
在代码中添加断点。你应该看看问题出在哪里。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
}
推荐阅读
- elixir - 灵药图绑定的最佳方法
- java - java中的加密/解密应用程序级集合
- javascript - 有没有办法知道用户结束的进程是否通过 Ctrl+C 在集成终端内运行?
- java - 通过正则表达式删除所有非打印字符
- wordpress - 是否有任何功能可以在&中包含类
- 标签 wp_nav_menu() 的后代?
- sql - 将结果从 1:n 查询移动到重复列
- c# - 自动突出显示 TextBox 或 RichTextBox 中的部分文本
- c# - 按未选择的列排序
- sparql - 通过 SPARQL 根据值查找所有可能的组合
- c# - 如何在 WPF 中使用 Datagrid 更新数据库