根据选择的 collectionview 单元格显示不同的 tableview 数据 [英] Display different tableview data depending on which collectionview cell is selected

查看:21
本文介绍了根据选择的 collectionview 单元格显示不同的 tableview 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是界面截图

我的 tableview 上方有一个 collectionview.我正在尝试根据选择的 collectionview 单元格更改 tableview 的数据.

I have a collectionview above my tableview. I'm trying to get the tableview's data to change based on which collectionview cell is selected.

此视图处理菜单项选择,菜单项显示在 tableview 中.这些菜单项的类别显示在 tableview 正上方的水平滚动 collectionview 中.

This view handles menu item selection, and menu items are displayed in the tableview. The categories for these menu items are displayed in a horizontally-scrolling collectionview right above the tableview.

餐厅类有一个实例变量 .categories,它返回一个字符串数组.这就是填充集合视图的内容.菜单项类有一个实例变量 .category,它返回一个字符串.我打算匹配这两个.

The restaurant class has an instance variable .categories which returns an array of strings. This is what populates the collectionview. The menu item class has an instance variable .category which returns a single string. I intend to match these two.

我的预期结果:对于 collectionview 中的每个选定单元格,获取其类别名称并将其传达给 tableview.tableview 应该使用这个名称并过滤它的菜单项以检查匹配的类别名称.显示那些特定的 tableview 单元格.

My intended result: For each selected cell in the collectionview, take its category name and communicate it to the tableview. The tableview should take this name and filter through its menu items to check for matching category names. Display those specific tableview cells.

对于我的收藏视图:


    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:

For my 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
    }

现在,我的结果(显然)是我的所有菜单项都显示在 tableview 中,无论选择哪个 collectionview 单元格.

Right now, my result (obviously) is that simply all of my menu items are being displayed in the tableview, regardless of which collectionview cell is selected.

推荐答案

您的问题需要以下两种格式.

Two things needed in following format for your question.

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

Make sure, restaurant?.categories data type as [String] and restaurant?.menuItems data type as [String : Any] . This is one of the simplest way to understand data transfer within UIViewController

这篇关于根据选择的 collectionview 单元格显示不同的 tableview 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆