【发布时间】:2016-10-27 12:17:09
【问题描述】:
我有一个UITableView这样的:
以及使用Alamofire 和SwiftyJson 从JSON 中获取tableView 数据:
JSON 数据:
{
"timeline": {
"Milan": {
"place": [
{
"name": "Place 1",
"kind": "historic",
},
{
"name": "Place 2",
"kind": "historic",
},
{
"name": "Place 3",
"kind": "historic",
},
{
"name": "Place 4",
"kind": "historic",
}
]
},
"Paris": {
"place": [
{
"name": "Place 1",
"kind": "historic",
},
{
"name": "Place 2",
"kind": "historic",
},
{
"name": "Place 3",
"kind": "historic",
}
]
}
}
}
我的问题是我如何将城市地点分隔在一个从名为 Place 的类继承的数组中,并将数据放入 tableView 中,如上图所示。
struct Place {
var name = ""
var type = ""
}
我在Alamofire.success写了一些代码,但这还不够:
if let jsonData = response.result.value {
let json = JSON(jsonData)
if let items = json["timeline"].dictionary {
for (key,subJson):(String, JSON) in items {
self.cityName.append(key)
...
}
}
}
【问题讨论】:
-
为什么要使用 for 循环,您只需要一个数组中的数据并将其填充到表中
标签: ios json swift uitableview uitableviewsectionheader