【发布时间】:2016-07-16 11:37:59
【问题描述】:
我在表格视图中获取Json 菜单数据。现在我需要在创建自定义单元格时将"Subcategory" 数据传递给另一个表格视图。我在didSelectRowAtIndexPath 的自定义单元格中获取了另一个表格视图。
我的json数据格式
[{
"id" :"233",
"name" :"dgghd",
"subcategory:[{
"id" :"233",
"name" :"dgghd",
"items:[{
"id" :"233",
"name" :"dgghd",
},... more items]
},....more subcategories
]
},....more main menus
]
获取菜单数据
Alamofire.request(.GET, myUrl)
.validate()
.responseJSON { response in
switch response.result
{
case .Success:
if let value = response.result.value {
let json = JSON(value)
// print(json)
//main menu section
for (_, content) in json {
let menu = Menu(id: Int(content["id"].stringValue),
name: content["name"].string,
image: content["image"].string,
coupon: content["coupon"].int,
icon: content["icon"].string,
order: Int(content["order"].stringValue),
aname: content["name"].string,
options: Int(content["options"].stringValue),
subcategory:content["subcategory"].arrayObject)
self.menus.append(menu)
}
for (_, sub) in json {
for (_, subcategory) in sub["subcategory"] {
let subcategory = SubCategory(
id: Int(subcategory ["id"].stringValue),
name: subcategory ["name"].string,
description: subcategory ["description"].string,
image: subcategory ["image"].string,
coupon: Int(subcategory ["coupon"].stringValue),
icon: subcategory ["icon"].string,
order: Int(subcategory ["order"].stringValue),
aname: subcategory ["aname"].string,
options: Int(subcategory ["options"].stringValue),
items: subcategory ["items"].arrayObject
)
self.subcategories.append(subcategory)
}
自定义单元创建代码
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menus.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Menucell", forIndexPath: indexPath)as! MENUTableViewCell
let menu: Menu = menus[indexPath.row]
cell.Ordermenu.text! = menu.name!
return cell
}
如何将子类别传递给自定义的“Menutableviewcell”,以便将子类别数据显示在各个部分中?
【问题讨论】:
标签: iphone json swift uitableview