swift - 根据选择的collectionview单元格显示不同的tableview数据
问题描述
我的 tableview 上方有一个 collectionview。我正在尝试根据选择的 collectionview 单元格来更改 tableview 的数据。
该视图处理菜单项选择,并且菜单项显示在表格视图中。这些菜单项的类别显示在 tableview 正上方的水平滚动 collectionview 中。
restaurant 类有一个实例变量.categories
,它返回一个字符串数组。这就是填充集合视图的内容。菜单项类有一个.category
返回单个字符串的实例变量。我打算匹配这两个。
我的预期结果:对于 collectionview 中的每个选定单元格,获取其类别名称并将其传达给 tableview。表格视图应采用此名称并通过其菜单项进行过滤以检查匹配的类别名称。显示那些特定的表格视图单元格。
对于我的收藏视图:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = restaurant?.categories.count {
return count
} else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! CategoryCell
// Sets label text and images for each category
cell.categoryLabel.text = restaurant?.categories[indexPath.row]
if UIImage(named: (restaurant?.categories[indexPath.row])!) != nil {
cell.buttonView.image = UIImage(named: (restaurant?.categories[indexPath.row])!)
}
return cell
}
对于我的表格视图:
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 currentItem = restaurant?.menuItems[indexPath.row]
cell.item = currentItem
return cell
}
现在,我的结果(显然)是我的所有菜单项都显示在 tableview 中,而不管选择了哪个 collectionview 单元格。
解决方案
您的问题需要以下格式的两件事。
1. restaurant?.categories 值:
[popular, milk tea, snacks, tea, ...]
2. restaurant?.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
集合视图
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
whichCellSelect = indexPath.item
yourTableView.reloadData()
}
表视图
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
推荐阅读
- vba - 无法通过 VBA 使用 selenium 在搜索框中输入值
- r - R 脚本在本地机器上成功运行,而不是在 EC2 实例上
- python - Python - 在 CSV 或 XLS 中匹配的正则表达式,并返回匹配的位置
- java - Android Studio androidx.coordinatorlayout error and how to set up 3Dot Menu?
- string - Oracle SQL:我应该如何识别字符串的长度?
- flash - Adobe Air 错误 #3001:文件或目录访问被拒绝
- python - Python中的偏相关
- docker - 如何在离线环境中同时使用 swagger-editor 和 swagger-codegen docker 镜像
- r - geom_dot 为不同的组提供不同的点大小并将一个放在前面
- r - R - 从现有列值创建和命名数据框