【问题标题】:How to control the contents of a table view based on cell selection in a collection view?如何根据集合视图中的单元格选择来控制表格视图的内容?
【发布时间】:2019-08-11 19:48:03
【问题描述】:

我在同一个视图控制器中有一个包含 4 个单元格的集合视图和一个表格视图。 CV 中的每个单元格必须使表格视图显示不同的列表;每个 CV 单元格有一种表格单元格类型。当用户点击一个 CV 单元格时,表格必须重新加载其数据并显示正确的单元格类型(基本上是带有自定义单元格的不同列表)。这些列表中的每一个仅包含 1 个部分。

我使用表格视图创建并注册了所有不同的单元格,并准备好所有数据,设置视图等等。当用户点击另一个 CV 单元格时,如何使表格视图丢弃其当前单元格(列表)并显示具有不同单元格的新列表? 我想解决方案在 CV 委托的 didSelectItem 方法中,但我找不到任何信息来显示当用户更改 CV 内的单元格选择时如何使表出列不同类型的单元格;或根据需要丢弃前一个。 目前我只注册和出列一种类型的单元格,在 CV 的委托方法中,我调用了应该将新列表放入表中的空函数。 每个列表的行数是动态的,这意味着我必须再次调用表视图上的委托方法。 我找到了 MVVM 模式的示例,但我无法将其应用于我的逻辑,因为它更加静态。
任何帮助将非常感激。谢谢。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let selectedMenuTab = indexPath.item
        switch selectedMenuTab { // show different type of cell.
        case 0: showAList()
        case 1: showBList()
        case 2: showCList()
        case 3: showDList()
        default:
            print("no such main tab")
        }
    }


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableOfConversations.dequeueReusableCell(withIdentifier: rowID, for: indexPath) as! ConversationsListCell
        let messageInChatList = listOfOneToOneChats[indexPath.item]
        cell.messageInChatList = messageInChatList
        cell.selectionStyle = .none
        return cell
    }


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listOfOneToOneChats.count
    }

【问题讨论】:

    标签: swift uitableview uicollectionview


    【解决方案1】:

    我认为这应该像在集合视图委托的didSelectItemAt 方法的末尾调用tableView.reloadData() 一样简单。

    tableView 的数据源应该基于一个普通的数组,例如:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return displayingList.count
    }
    

    然后在collection view的didSelect中设置那个数组然后告诉tableView重新加载:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let selectedMenuTab = indexPath.item
        switch selectedMenuTab { // show different type of cell.
        case 0: displayingList = listA
        case 1: displayingList = listB
        case 2: displayingList = listC
        case 3: displayingList = listD
        default:
            print("no such main tab")
        }
        tableView.reloadData()
    }
    

    对于出列单元格,根据类型检查它们是否不同,或者根据selectedMenuTab

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UICollectionViewCell
        let selectedMenuTab = indexPath.item
        switch selectedMenuTab { // show different type of cell.
        case 0: cell = tableOfConversations.dequeueReusableCell(withIdentifier: rowID, for: indexPath) as! ConversationsListCell
        //And so on
        default:
            fatalError("No cell for this tab")
        }
        let item = displayingList[indexPath.item]
        // Cell setup
        return cell
    }
    

    为了避免 Any 数组,需要考虑值的类型,这不是超级类型安全的,但这取决于对象的类型。

    【讨论】:

    • 谢谢。当我第一次设置表格视图的内容时,我想知道选择了集合视图的哪个单元格。因此我可以从一开始就用正确的数组设置它。我会在 section 方法的行数内使用 switch 语句。 CV 始终选择一个单元格;在创建第一个,然后是用户点击的任何内容。知道怎么做吗?
    • 不确定您是如何确定选定状态的,但我会在实例化时将 selectedMenuTab 变量默认为 0,然后让集合始终打开该变量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多