[英]How do I load a local JSON file into a uitableview in swift?
I've been trying to look for how to do it, but still have not been successful. 我一直在尝试寻找方法,但仍然没有成功。 I want to put a JSON file into a tableview 我想将JSON文件放入表格视图
{
"person":[
{
"name": "John",
"age": "17",
},
{
"name": "Bob",
"age": "23",
}
]
}
You can get the details from the json string as follows. 您可以从json字符串中获取详细信息,如下所示。
let listDetails = json["Person"] as? Array<[String:String]>)!
and you can have your table view delegates as follows. 您可以按如下方式让您的表视图委托。
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
}
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.