Две вещи, необходимые в следующем формате для вашего вопроса.
1. Ресторан?. значения категорий:
[popular, milk tea, snacks, tea, ...]
2. Ресторан?. Значения menuItems:
{
popular : [
{
"name" : "thai-tea",
"price": "$6",
"thumbnailURL" : "https://.../thai.png"
},
{
"name" : "milk-tea",
"price": "$3",
"thumbnailURL" : "https://.../ml.png"
}
],
snacks : [
{
"name" : "brownie",
"price": "$7",
"thumbnailURL" : "https://.../brw.png"
},
{
"name" : "pasta",
"price": "$3",
"thumbnailURL" : "https://.../pas.png"
}
]
}
Глобальная переменная
var whichCellSelect : Int = 0
CollectionView
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
whichCellSelect = indexPath.item
yourTableView.reloadData()
}
Tableview
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let count = restaurant?.menuItems.count {
return count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = masterTableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! MenuItemCell
cell.selectionStyle = UITableViewCell.SelectionStyle.none
let currentCategory = restaurant?.categories[whichCellSelect] // You will get String here
let currentMenuItem = restaurant?.menuItems[currentCategory] // You will get Array here
cell.item = currentItem
return cell
}
Примечания
Убедитесь, что тип данных restaurant? .Categories равен [String]
, а тип данных restaurant? .MenuItems - [String : Any]
. Это один из самых простых способов понять передачу данных в пределах UIViewController