首页 > 解决方案 > 根据选择的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 单元格。

标签: swiftuitableviewuicollectionviewcell

解决方案


您的问题需要以下格式的两件事。

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


推荐阅读