首页 > 解决方案 > 从 UITableView 中的多维数组传递数据

问题描述

我有一个看起来像这样的数组:

[Category(title: "First Category", question: [
     Question(
 title: "1 Question",
 article: "1 Arcticle",
 anwser: "Answer",
 link: "google.com",
 favorite: false,
 possibleAnswers: ["First", "Second", "Third", "Fourth"],
 rightAnswerNumber: 2), etc..]),
etc..]

我不明白如何在下一个 ViewController 中提取数据。在这里我有一个代码在父母:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == ListViewController.className() {
        let indexPath = tableView.indexPathForSelectedRow
        let destinationVC = segue.destination as? ListViewController
        var data: Category
        if isFiltering() {
            data = filteredData[indexPath!.row]
        } else {
            data = dataSource[indexPath!.row]
        }
        destinationVC?.passedData = data
    }
}

在儿童中:

var passedData: Category?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: ListTableViewCell.identifier(), for: indexPath) as! ListTableViewCell
    cell.passedData = passedData![indexPath.row][indexPath.row] //Here
    cell.selectionStyle = .gray
    return cell

}

我需要让 cell.passedData 从 indexPath 获取 Category 中的 Question 数组,但不知道如何。

我正在考虑使用 for 循环从 Category 中提取数组,但没有意识到如何。

标签: iosswiftuitableview

解决方案


由于passedData子视图控制器的类型为Category,您只需要替换

cell.passedData = passedData![indexPath.row][indexPath.row] //Here

cell.passedData = passedData!.question[indexPath.row]

在子视图控制器中


推荐阅读