ios - 从嵌套数据数组将数据填充到 UITableViewCell 内的 UITableView
问题描述
我是 swift 和 iOS 编程的新手。我正在尝试制作一个两层表格视图。表格视图单元格内的表格视图基本上就是我想说的。主表视图工作正常,但表视图内的表视图根本不工作..我已经尝试了一切......
# 楷模
// Main Model Struct
struct MainModel {
private(set) public var title: String
private(set) public var document: [Documents] = [Documents]()
init(title: String, document: [Documents]) {
self.title = title
self.document = document
}
}
// Document model struct
struct Documents {
private(set) public var name: String
private(set) public var link: String
init(name: String, link: String){
self.name = name
self.link = link
}
}
# 数据服务类
class DataService{
static let instance = DataService()
private let dataArray = [
MainModel(title: "Demo 1", document: [Documents(name: "DOC 1", link: "http://www.google.com"), Documents(name: "DOC 2", link: "http://www.facebook.com")])
]
func getArrays() -> [MainModel] {
return dataArray
}
}
# 主视图控制器
import UIKit
class ResourceDataVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var dataTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.dataTable.delegate = self
self.dataTable.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataService.instance.getArrays().count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: “mainCell”) as? MainTableViewCell {
let _item = DataService.instance.getArrays()[indexPath.row]
cell.updateCell(data: _item)
return cell
} else {
return ResourceCell()
}
}
}
# 主 TableViewCell 类
import UIKit
class MainTableViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var title: UILabel!
@IBOutlet weak var documentTable: UITableView!
var documentRes: [Documents] = [Documents]()
override func awakeFromNib() {
super.awakeFromNib()
self.documentTable.delegate = self
self.documentTable.dataSource = self
}
func updateCell(data: MainModel) {
self.title.text = data.title
self.documentRes = data.document
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.documentRes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: “documentTableCell”) as? DocumentCell {
let _doc = documentRes[indexPath.row]
cell.updateDocument(document: _doc)
return cell
} else {
return DocumentCell()
}
}
}
# 文档 TableViewCell 类
import UIKit
class DocumentCell: UITableViewCell {
@IBOutlet weak var docName: UILabel!
@IBOutlet weak var docLink: UILabel!
func updateDocument(document: Documents) {
self.docName.text = document.name
self.docLink.text = document.link
}
}
而且这个方法永远不会被调用......为什么?我想这可能是问题所在...
// from: MainTableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: “documentTableCell”) as? DocumentCell {
let _doc = documentRes[indexPath.row]
cell.updateDocument(document: _doc)
return cell
} else {
return DocumentCell()
}
}
请帮我 !
解决方案
在结束updateCell
时MainTableViewCell
self.documentTable.reloadData()
推荐阅读
- javascript - 将鼠标悬停在 div 上时显示其转换
- c# - Discord 机器人不发送图像
- oracle - Oracle 版本 19 上的快速刷新物化视图
- python - 如何让 AWS Chalice 使用 pyenv virtualenv python 版本?
- parsing - 如何根据一组 ABNF 语法规则对输入进行标记
- c++ - 如何访问地图的值
> 在 C++ 中? - c - 精度为 0.00001 的泰勒级数(打印与用户输入的值相同的值)C 代码
- hibernate - Hibernate 从其他表中获取字段
- python - 我正在尝试使用 sklearn.neighbors.KernelDensity 查找分类器错误
- spring-boot - 使用 Set 数据结构管理 JPA 实体 - 使用 add 方法