【发布时间】:2018-08-23 05:59:45
【问题描述】:
我有一个模型类数组,这个模型类有一个数组。我想从数组填充表格视图。它向我显示错误
这是我的模型类
class DataModel {
var name:[String]
init(name:[String]) {
self.name = name
}
}
这是我的模型对象:
class Data {
static var product = [DataModel]()
}
这是我将数据附加到对象的函数:
class Function {
static func read(compleationHandler:@escaping ()->()) {
var model = ["nsu","iUB","UIU"]
DispatchQueue.global(qos: .userInteractive).async {
if Data.product.count == 0 {
Data.product.append(DataModel(name: model))
}
DispatchQueue.main.async {
compleationHandler()
}
}
}
}
这是我的 tableView Cell 方法:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "myCell") as! myCell
cell.textLabel?.text = Data.product[indexPath.row].name[indexPath.count]
return cell
}
这是我的 ViewDidLoad :
myTableView.delegate = self
myTableView.dataSource = self
Function.read { [weak self] in
print(Data.product.count)
self?.myTableView.reloadData()
}
}
【问题讨论】:
-
错误是什么?它在哪里显示?
-
“线程 1:致命错误:索引超出范围”
-
我想从这个数组中填充属于模型“cell.textLabel?.text = Data.product[indexPath.row].name[indexPath.row]”的数据
-
这可能是因为您在 cellForRowAt 中使用了错误的索引。
cell.textLabel?.text = Data.product[indexPath.section].name[indexPath.row]但我不能确定,除非我看到你的 numberOfSections 和 numberOfRows 代表。
标签: ios swift uitableview