Swift 从数组中解析的值未显示在tableview中
我需要在表行中显示值->“所有[0].id”。 该值是JSON文件中的ID123456。 生成的代码没有错误,但表行中仍然没有数据 谢谢你的建议 我用以下值解析了JSON文件:Swift 从数组中解析的值未显示在tableview中,swift,Swift,我需要在表行中显示值->“所有[0].id”。 该值是JSON文件中的ID123456。 生成的代码没有错误,但表行中仍然没有数据 谢谢你的建议 我用以下值解析了JSON文件: { "fields": { "123456": { "timestampValue": "2019-03-05T23:00:00Z" }, "7895": { "timestampValue": "2019-03-02T23:00:00Z" } },
{
"fields": {
"123456": {
"timestampValue": "2019-03-05T23:00:00Z"
},
"7895": {
"timestampValue": "2019-03-02T23:00:00Z"
}
},
"createTime": "2019-03-08T00:14:55.357221Z",
"updateTime": "2019-03-08T17:22:08.398718Z"
}
有一个带有tableview的我的ViewController文件:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let data = DataLoader().userData
let all = [Item]()
@IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return all.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = all[0].id
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
}
在DataLoader.swift中有我的代码:
public class DataLoader
{
@Published var userData = [Root]()
init()
{
load()
}
func load()
{
if let fileLocation = Bundle.main.url(forResource: "mydata", withExtension: "json")
{
// Do catch in case of error
do
{
let data = try Data(contentsOf: fileLocation)
let jsonDecoder = JSONDecoder()
let res = try jsonDecoder.decode(Root.self, from: data)
var all = [Item]()
for (id,item) in res.fields {
all.append(Item(id:id,timestampValue:item.timestampValue))
}
print(all)
}
catch
{
print(error)
}
}
}
}
这个项目里有什么?是空的吗?这件物品的数量是多少?另外,在
delegate
方法上方编写viewLifeCycle
方法。在“<代码>扩展> <代码> >中,您可以在项目12345和项目计数中显示您在“代码>项目< /代码>中添加了什么位置?导入基础结构根:可允许{字段:[字符串:字段],允许创建时间,UpDeDeTime:String }//Mark:-FieldStultField:可允许{TimeStimpPald:String }struct Item{let id,timestampValue:String}不,我的意思是你在哪里添加了这些值?只要把全部打印出来
,然后告诉我。