我有一个看起来像这样的数组:
[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,但不知道如何。
我当时正在考虑使用for循环从Category中提取数组,但没有意识到如何实现。
答案 0 :(得分:1)
由于子视图控制器的passedData
的类型为Category
,因此只需替换
cell.passedData = passedData![indexPath.row][indexPath.row] //Here
与
cell.passedData = passedData!.question[indexPath.row]
在子视图控制器中