我有这样的JSON数据:
[{"name":"Rice","Menu":{"title":"titl1"}},{"name":"Name2","Menu":{"title":"title2"}}]
我想构建一个表视图,每个对都有一个部分[id,name]。部分标题应该是菜单标题,每个部分的唯一单元格应该是名称值。
如何将JSON数据解析为数组并使用array.count来确定必须显示多少部分?
修改
class Menus {
private var _name:String!
private var _title:String!
var name:String {
return _name
}
var title:String {
return _title
}
init(nam:String, title:String) {
_name = nam
_title = title
}
}
新编辑
x.forEach {
if let uw = ($0["name"]).string {
um.insert(uw)
if let hru = ($0["Menu"]["title"]).string {
us.insert(hru)
}
}
}
for i in um {
for u in us {
var men = Menus(nam: i, tit: u)
self.menus.append(men)
}
}
答案 0 :(得分:0)
var arrData:[[String:AnyObject]]! = [
["name":"Rice",
"Menu":
[
"title":"titl1"
]
],
[
"name":"Name2",
"Menu":
[
"title":"title2"
]
]
]
var arrMenu:[Menus]! = []
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<arrData.count {
let dish = arrData[i]
let menu = dish["Menu"] as! [String:String]
let name = dish["name"] as! String
let title = menu["title"]
arrMenu.append(Menus.init(nam: name, title: title!))
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return arrMenu.count
}
override func tableView( tableView :UITableView, titleForHeaderInSection section: Int)->String{
let menu:Menus = arrMenu[section]
return menu.name
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let menu = arrMenu[indexPath.section]
cell.textLabel?.text = menu.title
return cell
}