ios - 从 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 中提取数组,但没有意识到如何。
解决方案
由于passedData
子视图控制器的类型为Category
,您只需要替换
cell.passedData = passedData![indexPath.row][indexPath.row] //Here
和
cell.passedData = passedData!.question[indexPath.row]
在子视图控制器中
推荐阅读
- mongodb - 从嵌入式 Mongo 文档中以字符串形式获取数据
- java - 运行Junit测试时如何修复assertTrue错误
- angular - 如何自定义 Compodoc Angular 文档?
- jquery - 如何避免 .each 方法中的重复值
- php - 如何在 HMVC Codeigniter 中获取模块名称
- json - 如何将特定 DBpedia (SPARQL) 导出为 JSON 以用于 openlayers 地图?
- android - webkit 未定义:InAppBrowser Cordova
- webpack - Webpack 4 的 @atlaskit/editor-core 包大小问题
- ios - 如何在代理设置后面使用 Google Speech to Text API?
- html - 按内容拉伸 bootstrap3 列