[英]Binding JSON data to TableView and CollectionView
I am trying to create a table view with a collection view in each cell.我正在尝试在每个单元格中创建一个带有集合视图的表格视图。 My JSON structure is like Main Menu title for the tv cell and in each main object, I receive a submenu, whose data I want to use in the collection view.
我的 JSON 结构类似于电视单元的主菜单标题,在每个主 object 中,我收到一个子菜单,我想在集合视图中使用其数据。
The issue I am facing is in using indexes of table cell and collection cell to get the nested data.我面临的问题是使用表格单元格和集合单元格的索引来获取嵌套数据。 Here is my JSON
这是我的 JSON
"MainMenu": [
{
"ID": 8,
"MasterID": 12,
"Title": "Payment",
"Image": "image.png",
"SubMenu": [
{
"ID": 9,
"Title": "My Bill",
"Image": "image.png"
},
{
"ID": 10,
"Title": "Electricity ",
"Image": "image.png"
},
{
"ID": 11,
"Title": "Gas",
"Image": "image.png"
},
{
"ID": 12,
"Title": "Telephone",
"Image": "image.png"
},
{
"ID": 13,
"Title": "Water",
"Image": "image.png"
}
]
},
{
"ID": 40,
"MasterID": 14,
"Title": "Services",
"Image": "image.png",
"SubMenu": [
{
"ID": 32,
"Title": "Upload Slip",
"Image": "image.png"
},
{
"ID": 41,
"Title": "SRY (Self Reliant Youth)",
"Image": "image.png"
},
{
"ID": 42,
"Title": "Dollor Product Purchase ",
"Image": "image.png"
},
{
"ID": 43,
"Title": "ARY Coin Redemption",
"Image": "image.png"
},
{
"ID": 44,
"Title": "Pay Via QR",
"Image": "image.png"
},
{
"ID": 45,
"Title": "Card Request",
"Image": "image.png"
}
]
},
{
"ID": 14,
"MasterID": 8,
"Title": "Mobile Balance",
"Image": "image.png",
"SubMenu": [
{
"ID": 15,
"Title": "My Mobile",
"Image": "image.png"
},
{
"ID": 16,
"Title": "Mobilink",
"Image": "image.png"
},
{
"ID": 17,
"Title": "Telenor",
"Image": "image.png"
},
{
"ID": 18,
"Title": "Ufone",
"Image": "image.png"
},
{
"ID": 19,
"Title": "Warid",
"Image": "image.png"
},
{
"ID": 20,
"Title": "Zong",
"Image": "image.png"
}
]
},
{
"ID": 46,
"MasterID": 6,
"Title": "Help",
"Image": "image.png",
"SubMenu": [
{
"ID": 47,
"Title": "Call",
"Image": "image.png"
},
{
"ID": 48,
"Title": "Chat",
"Image": "image.png"
},
{
"ID": 49,
"Title": "Email",
"Image": "image.png"
},
{
"ID": 50,
"Title": "Others",
"Image": "image.png"
},
{
"ID": 51,
"Title": "VAS / SMS",
"Image": "image.png"
}
]
},
{
"ID": 21,
"MasterID": 5,
"Title": "Fund Transfer",
"Image": "image.png",
"SubMenu": [
{
"ID": 22,
"Title": "To Other ARY Wallet",
"Image": "image.png"
},
{
"ID": 23,
"Title": "To Bank Account",
"Image": "image.png"
}
]
},
{
"ID": 34,
"MasterID": 1,
"Title": "$ & MilliGold Deal",
"Image": "image.png",
"SubMenu": [
{
"ID": 35,
"Title": "$ Deposit",
"Image": "image.png"
},
{
"ID": 36,
"Title": "$ Purchase",
"Image": "image.png"
},
{
"ID": 37,
"Title": "$ Time Games",
"Image": "image.png"
},
{
"ID": 38,
"Title": "Scan for Bid",
"Image": "image.png"
}
]
}
],
"CustomerAccountInformation": []
}
Please guide me how can I use my JSON objects each in different tablecell.请指导我如何在不同的表格单元中使用我的 JSON 对象。 Here is what I have tried:
这是我尝试过的:
TVCell
cell.titleLabel.text = menu?.mainMenu?[indexPath.row].title ?? "Our services"
CollectionCell
cell.image.af_setImage(withURL: URL.init(string: (menu?.mainMenu?[indexPath.row].subMenu?[indexPath.row].image)!)!)
First, in your tableview cell, set the tag of the collectionview to indexPath.row首先,在您的 tableview 单元格中,将 collectionview 的标签设置为 indexPath.row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : TVCell = tableview.dequeueReusableCell(withIdentifier: "TVCell", for: indexPath) as! TVCell
cell.myCollectionView.tag = indexPath.row
return cell
}
Now in your collectionview cell you can parse the data like following现在在您的 collectionview 单元格中,您可以解析数据,如下所示
let index = cell.tag
cell.image.af_setImage(withURL: URL.init(string: (menu?.mainMenu?[index].subMenu?[indexPath.row].image)!)!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.