根据选择哪个collectionview单元格显示不同的tableview数据

陈大卫

这是用户界面的屏幕截图

我在表格视图上方有一个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单元,我的所有菜单项都只显示在表视图中。

McDonal_11

按照以下格式,您的问题需要做两件事。

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过从tableView的标头collectionView中选择不同的单元格来更改tableView单元格

在不同的 collectionView 单元格内填充不同的 tableView

无法在TableView单元格中选择CollectionView

根据单元格选择查询数据

iOS Swift Tableview:-如何根据内容显示单元格?

仅根据突出显示的单元格选择值

Tableview重用单元格并首先显示错误的数据

Swift UICollectionview水平单元格显示中心位置,并根据单元格选择自动移动

Xib的collectionview单元格未显示

VBA:根据标准在单元格中显示数据

根据单元格增加TableView的高度

TableView上的多个单元格选择

Swift 3如何在CollectionView中选择单元格时设置不同的背景色

TableView 显示空单元格

TableView的单元格未显示

Tableview显示错误的单元格大小

如果在 Swift 3 中未选择 CollectionView 单元格,如何显示警报?

在tableview单元格内创建动态collectionView

如何获取在tableview单元格内的collectionview单元格的按钮单击上的索引

将目标添加到 tableview 单元格内的 collectionview 单元格的按钮

ios:在点击collectionview单元格或tableview单元格时禁用第二触摸事件

根据文本突出显示单元格

根据价值突出显示单元格

根据条件突出显示单元格

如何根据内容设置collectionview单元格宽度?

根据按钮数量设置CollectionView单元格高度

尝试根据另一个单元格中的数据查询选择的单元格

如何获取在TableView单元格中显示的数据并将其显示在文本字段中?

Swift / TableView可重用单元格每次显示时都会加载不同的内容