ios - 在 JSON 下载 Swift 4 后从分段表视图中获取正确的索引行
问题描述
JSON 数据在下载之前已经按字母顺序排列。该应用程序正确地将数据划分为多个部分,根据第一个字母创建部分标题,并在正确的部分中列出以该字母开头的所有名称。问题是一旦转换到详细视图控制器,每个部分都会重复相同的数据。B、C 等部分显示所有正确的名称,但在进入详细视图控制器时重复“A”名称。如何让选定的单元格和详细信息视图控制器再次匹配?
func numberOfSections(in tableView: UITableView) -> Int {
return figuresByLetter.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return figuresByLetter[section].key
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return figuresByLetter[section].value.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "figureCell", for: indexPath)
cell.textLabel?.text = figuresByLetter[indexPath.section].value[indexPath.row].name
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? FigureViewController {
destination.figure = figures[(tableView.indexPathForSelectedRow?.row)!]
}
}
让我知道是否需要任何其他代码来回答问题!
struct FigureStats: Decodable {
let name: String
let number: String
let weapon: String?
let desc: String?
let year: String?
}
在详细视图控制器中:
class FigureViewController: UIViewController {
var figure:FigureStats?
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = figure?.name
numberLabel.text = figure?.number
weaponLabel.text = figure?.weapon
descLabel.text = figure?.desc
yearLabel.text = figure?.year
}
}
解决方案
尝试使用此属性。我希望它对你有帮助。
class YourClass: UIViewController {
var currentFigure: FigureStats!
func numberOfSections(in tableView: UITableView) -> Int {
return figuresByLetter.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return figuresByLetter[section].key
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return figuresByLetter[section].value.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "figureCell", for: indexPath)
cell.textLabel?.text = figuresByLetter[indexPath.section].value[indexPath.row].name
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
currentFigure = figuresByLetter[indexPath.section].value[indexPath.row]
print(currentFigure)
performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? FigureViewController {
print(currentFigure)
destination.figure = currentFigure
}
}
class FigureViewController: UIViewController {
var figure:FigureStats?
override func viewDidLoad() {
super.viewDidLoad()
print(figure)
nameLabel.text = figure?.name
numberLabel.text = figure?.number
weaponLabel.text = figure?.weapon
descLabel.text = figure?.desc
yearLabel.text = figure?.year
}
}
推荐阅读
- node.js - 节点模块可以需要一个香草 js 文件吗?
- java - 如何从 Java/Android 中自动获取笔记本电脑的 IP 地址
- c# - 使用HTML提交后如何返回上一个视图
- javascript - 停止视频 3 秒,然后继续
- gradle - 防止 gradle 跳过任务
- c# - Google Data .NET API 安装程序 - 声称我需要 .NET 2.0
- apache-spark - Spark 流式查询两次加载数据源
- postgresql - 如何创建将父 ID 和祖父母 ID 推送到数组中的递归 cte 查询
- r - 获取 dplyr 管道结构中最常出现的因子级别
- javascript - 在添加订单项之前验证脚本中的状态 (NetSuite)