gpt4 book ai didi

ios - 如何快速将本地 JSON 文件加载到 uitableview 中?

转载 作者:行者123 更新时间:2023-11-28 13:01:18 28 4
gpt4 key购买 nike

我一直在尝试寻找如何去做,但仍然没有成功。我想把一个 JSON 文件放到一个 tableview 中

{
"person":[
{
"name": "John",
"age": "17",
},
{
"name": "Bob",
"age": "23",
}

]
}

最佳答案

您可以从json字符串中获取详细信息,如下所示。

let listDetails = json["Person"] as? Array<[String:String]>)!

你可以让你的 TableView 委托(delegate)如下。

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return listDetails.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("yourCellIdentifier", forIndexPath: indexPath)

let dict = listDetails[indexPath.row] as? [String:String]
let name = dict["name"]
let age = dict["age"]

//now assign this name and age to the textViews you have in your tableView.

return cell
}

希望对您有所帮助。

关于ios - 如何快速将本地 JSON 文件加载到 uitableview 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33748496/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com