我在表格 View 中获取 Json
菜单数据。现在我需要在创建自定义单元格时将“Subcategory”数据传递到另一个表格 View 。我在 didSelectRowAtIndexPath
上的自定义单元格中获取了另一个表格 View 。
我的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”,以便我可以将子类别数据显示在各个部分中?
最佳答案
如果我没猜错的话,那么您已经在某处创建了一个名为 SubCategory 的类。如果是这种情况,您可以在自定义单元格内创建一个具有 SubCategory 类型(可选)的 var,并且在创建单元格时,您可以传递您在自定义单元格内收到的 SubCategory 并根据需要在其中使用它。
您的自定义单元格类似于
class MENUTableViewCell: UITableViewCell {
var subCat: SubCategory?
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
当你创建单元格时会是这样的
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Menucell", forIndexPath: indexPath)as! MENUTableViewCell
cell.SubCategory = subcategories[indexPath.item]
return cell
}
您可以对菜单执行相同的操作,这样您就可以在单元格内传递所有数据并处理其中的所有内容。
编辑:您可以将 didSet 用于 subCat 变量,如下所示:
var subCat: subCategory? {
didSet{
if let name = subCategory?.name {
nameLabel.text = name
}else {
nameLabel.text = "Some default text"
}
}
这是更安全的方法,因此如果 subCategory 为零,您的应用程序不会崩溃,您可以进一步检查代码以了解为什么它为零。
关于iphone - 如何在 Swift 中将字典数组从一个 TableView 传递到另一个自定义 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38410809/