【问题标题】:Display different tableview data depending on which collectionview cell is selected根据选择的collectionview单元格显示不同的tableview数据
【发布时间】:2019-06-27 22:30:02
【问题描述】:

Here's a screenshot of the UI

我的 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


【解决方案1】:

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

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

CollectionView

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内数据传输的最简单方法之一

【讨论】:

  • 也请支持我的回答。这对我们的 SO 追随者很有用
  • 如果集合视图启用多项选择,您知道如何实现这一点吗?
  • 是的,我们可以!在此处发布您的问题并分享问题链接。@alobaili
【解决方案2】:

不同的类别是如何存储的?您是否为每个类别使用一个数组,并在选择 CollectionView 中的一个选项后将该特定数组发送到 TableView?然后,您需要刷新 TableView,以便将更改反映在界面中。

我也看不到用户在 CollectionView 中按下的内容在哪里。 ...你不能用这样的东西吗...

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}

要获得他们选择的内容,请使用该选项来更新表格,方法是写入不同的数组或根据类别仅添加某些数组元素,然后使用reloadData() 刷新表格。

【讨论】:

  • 嘿乔丹,刚刚更新了我的帖子,附上了我的观点截图;希望有点帮助。正如我在问题中所说,类别存储为字符串数组。每个单元格将其标签设置为字符串,并将存储在资产中的图标与字符串匹配。我当前的实现实际上并没有使用菜单项的类别。您的建议与我一直在尝试的方式一致,并且在理论上可行。然而,它的实际实现要困难得多,我不太确定如何去做。
猜你喜欢
  • 2020-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多