![](/img/trans.png)
[英]How to display iOS tableview from array data on button press - Swift
[英]How to display the data in the tableview cell from the array of array in swift
我的值为:-
[[yes,no],[yes,no]]
我需要在tableview的cellForRowAt indexPath中显示。
我的代码:
var tableArray:Array<[String]> = []
从JSON获取数据:-
if let data = wholedata["data"] as? Array<[String:Any]>{
print(data)
print(response)
for question in data {
let options = question["options"] as! [String]
self.tableArray.append(options)
// self.fetchedHome.append(options as! [home])
self.no = options.count
}
print(self.tableArray)
我的单元格在tableview的索引处:-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "Cell"
var cell: QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell
if cell == nil {
tableView.register(UINib(nibName: "QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? QuestionListCell
}
print(reviewViewModel.tableArray)
cell.question.text = "hai"
} else {
return UITableViewCell()
}
}
如何在表格视图单元格中显示数据?
首先,不要在cellForRow
注册该单元格,而应在viewDidLoad
注册它。
private let identifier = "Cell" // Declare the identifier as private constant on the top level of the class.
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: "QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
// do other stuff
}
然后在cellForRow
,从数据源数组获取索引路径的项目。 由于选项是一个数组,因此必须在两个标签中显示它或将它们连接在一起。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier) as! QuestionListCell
let options = tableArray[indexPath.row]
cell.question.text = options.joined(separator: ", ")
return cell
}
我猜您想显示的数据比options
。 但是目前,您仅使用options
填充数据源阵列
再次如我对先前问题之一的回答中所建议,我强烈建议将JSON解码为自定义结构。 它使生活变得如此轻松。
这是很容易的问题,兄弟。
如果您不介意使用部分,请尝试此〜!!!
var tableArray:Array<[String]> = []
func viewDidLoad() {
let table = UITableView()
table.register(YourNib, forCellReuseIdentifier: YourIdentifier)
}
func numberOfSections(in tableView: UITableView) -> Int {
return tableArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableArray[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let tempArray = tableArra[indexPath.section] as? [String],
let cell = tableView.dequeueReusableCell(withIdentifier: YourIdentifier) as? QuestionListCell {
// just Initialize your cell with data in datasource
cell.question.text = tempArray[indexPath.row]
return cell
} else {
return UITableViewCell()
}
}
您可以在显示在表中之前将array of array
转换为array
,并且tableViewController将仅具有数据array
。
您可以使用flatmap / compactMap将array of array
转换为array
:
let a = ["yes","no"]
let b = [a, a] // [["yes","no"], ["yes","no"]]
b.flatMap({$0}) // ["yes","no", "yes","no"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.