首页 > 解决方案 > 在 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
    }
}

标签: iosswift

解决方案


尝试使用此属性。我希望它对你有帮助。

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
    }
}

推荐阅读