我在表格视图上方有一个collectionview。我正在尝试根据选择哪个collectionview单元来更改tableview的数据。
该视图处理菜单项的选择,并且菜单项显示在表格视图中。这些菜单项的类别显示在表格视图正上方的水平滚动集合视图中。
restaurant类具有一个实例变量.categories
,该实例变量返回一个字符串数组。这就是填充collectionview的内容。菜单项类具有一个.category
返回单个字符串的实例变量。我打算匹配这两个。
我的预期结果:对于collectionview中的每个选定单元格,获取其类别名称并将其传达给tableview。表格视图应使用此名称,并在其菜单项中进行过滤以检查匹配的类别名称。显示那些特定的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
}
对于我的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 currentItem = restaurant?.menuItems[indexPath.row]
cell.item = currentItem
return cell
}
现在,我的结果(很明显)是,无论选择哪个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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句