0
我从tableview中获取Json
菜单数据。现在我需要在创建自定义单元格时将"Subcategory"
数据传递给另一个tableview。我在didSelectRowAtIndexPath
的自定义单元格中看了另一个tableview。如何将字典数组从一个tableview传递到另一个自定义tableview中Swift
我的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”,以便我可以在子类别数据中显示子节点?
但我越来越无价值的subCat当我试图显示它的值在MENUTableViewCell像这样http://pastie.org/10909609 –
而在你的代码,我认为,而不是cell.SubCategory ....细胞。应该使用subCat,因为你已经创建了SubCat作为SubCategory类型 –
我编辑了我的答案,以便你可以使用didSet安全地解开你传递的subCategory var,并在它为零时处理它。我想你必须检查你的代码,所以当单元格被创建时,你已经收到了json数据。 –