【发布时间】:2019-06-27 22:30:02
【问题描述】:
我的 tableview 上方有一个 collectionview。我正在尝试根据选择的 collectionview 单元格来更改 tableview 的数据。
此视图处理菜单项选择,并且菜单项显示在表格视图中。这些菜单项的类别显示在 tableview 正上方的水平滚动 collectionview 中。
餐厅类有一个实例变量.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 单元格。
【问题讨论】:
-
print(restaurant?.categories) ,这将给出,[流行,奶茶,小吃,茶,...] [来自您的屏幕截图]。我说的对吗?
标签: swift uitableview uicollectionviewcell