如何在 swift 中显示和隐藏 table 视图单元格

How to display and hide table view cells in swift

我有一个带有两个不同自定义单元格的 tableView。一个单元格的文本字段数据很少,而其他单元格也有不同的文本字段数据,这里我需要折叠并隐藏每个单元格。我不知道该怎么做 this.Please 让我知道。

https://maniacdev.com/2013/03/an-open-source-ios-control-allowing-you-to-create-great-looking-collapsible-lists-without-uitableview

请参考以上link,我想要swift中的相同内容。

提前致谢

所以你想隐藏你的 UITableViewCells 之一?

我认为最简单的方法是向您的 "Row Data Array" 添加更多信息。

例如:

您有 10 个列,您想隐藏 4 个特定列。您有一个名为 "getRowData" 的函数 - 此 returns(未选择过滤器时)所有 10 行。当您点击一个按钮时,您可以将 "getRowData" 函数更改为 return 只有您想要的 4 行。

对于上面显示的示例 - 您需要一些基于 UITableViewController 的可折叠列表。

所以你有(在这个例子中)3 个主要类别 - 如果用户点击一个 "Title" 你想展开这个类别的列表。

因此您可以使用版块 - 为每个类别创建一个版块。然后实现这样的东西:

// 为你的 "activated Category" 创建一个主变量

var enabledCategory:Int = 0 // you could add i base value

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let indexPath = tableView.indexPathForSelectedRow();

        enabledCategory = indexPath.row;
....

在此处检查用户点击了哪个部分。如果(例如“0” - 所以第一个,展开这个部分。所以做这样的事情:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if(indexPath.section == enabledCategory) {
        return rows // return rows you want to show
    }

您还需要更改此处的行数:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 3
}

这应该(在您的示例中)始终为 3。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

这取决于您的部分 // 类别