Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中隐藏UITableViewSections?_Ios_Uitableview_Swift - Fatal编程技术网

Ios 如何在Swift中隐藏UITableViewSections?

Ios 如何在Swift中隐藏UITableViewSections?,ios,uitableview,swift,Ios,Uitableview,Swift,我有一个静态分组表视图,它有5个部分(所有部分都有页眉,没有页脚)。我用故事板创造了这一切。现在,如何隐藏第一个/顶部UITableViewSection(包括标题)。我尝试创建UITableViewSection的出口,但它告诉我该出口无效(未声明类型): 我这样做是因为我计划做: section.hidden = true 不能这样做吗 我的委托和数据源设置100%正确。Swift 5: override func tableView(_ tableView: UITableView, n

我有一个静态分组表视图,它有5个部分(所有部分都有页眉,没有页脚)。我用故事板创造了这一切。现在,如何隐藏第一个/顶部UITableViewSection(包括标题)。我尝试创建UITableViewSection的出口,但它告诉我该出口无效(未声明类型):

我这样做是因为我计划做:

section.hidden = true
不能这样做吗


我的委托和数据源设置100%正确。

Swift 5:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return section == 4 ? 0 : return super.tableView(tableView, numberOfRowsInSection: section)
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return section == 4 ? 0.1 : super.tableView(tableView, heightForHeaderInSection: section)
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return section == 4 ? 0.1 super.tableView(tableView, heightForFooterInSection: section)
}
您可以使用委托方法
heightForHeaderInSection
:

早于Swift 5:使用
UITableView自动维度
代替
UITableView。自动维度

如果未使用高度
0.0
,请使用高度
0.1

如果不希望特定节中有单元格,请使用委托方法:

func numberOfRowsInSection(section: Int) -> Int {
  if (section == 0) {
    return 0
  }
  else {
  // return the number of rows you want
  }
}
或更整洁的
开关盒
语法:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    switch section {
    case 0:
        return 0.0
    default:
        return UITableView.automaticDimension
    }
}

我测试了这两个,它们运行良好。

0.0对我不起作用。我必须这样做,以使它的工作

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

    switch section {
    case 0:
        return 0.01
    default:
        return UITableViewAutomaticDimension
    }
}

对于那些因为使用静态分组表视图和动态节数而想要隐藏节的人,下面的解决方案可能会有所帮助。在我的例子中,每个要显示数据的部分都需要有一个自定义标题。任何没有数据的部分都需要完全隐藏

上面的答案在我的场景中非常有用。然而,对于那些并不总是知道哪些部分需要隐藏的人来说,这里是一个解决方案,您可以在此基础上进行扩展

在我的场景中,一个数组中最多有12个条目,我希望在最多12个部分中显示(在分组表视图的其他部分中)。如果要显示的条目少于12个,我希望通过给它们0个高度和0行来隐藏不必要的部分。我还想把头像藏起来

为此,我做了以下工作:

  • 按照@sasquatch给出的优秀答案设置您的tableView 上面
  • 在numberOfRowsInSection(section:Int)和tableView中(_ tableView:UITableView,heightForHeaderInSection:Int)函数,检查行/高度是否应为0 在我的例子中,我使用了第1-12节作为我的动态数据,所以我使用了如下代码:

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //For section 0 and 13, just 1 row is ok
        if section == 0 || section == 13 {
            return 1
        }
    
        //For sections 1 - 12, determine if we have data to populate it, or if we should hide it
        if section <= dynamicDataToDisplay.count {
            return 2
        }
    
        //If it's section 1 - 12, but we don't have a corresponding data entry in dynamicDataToDisplay, then just return 0 rows
        return 0
    
    }
    
    即使在设置了这些函数之后,我发现我仍然可以看到我想要隐藏的部分的标题。我想如果numberOfRows为0,heightOfHeader也为0,则不会调用viewForHeaderInSection,但它仍在被调用

    添加以下内容有助于确保不会不必要地创建标题:

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        //set up header for dynamic data sections
        if 1 ... 12 ~= section  {
    
            if tableView.numberOfRows(inSection: section) == 0 {
                return nil
            }
            //Continue with the header set up for valid sections with rows to display data
    
            ......
        }
    }
    

    此解决方案可能会帮助任何仍在创建页眉的人,尽管页眉的高度和行数设置为0。

    对于具有静态单元格的组UITableView,只有此解决方案有效:

    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        let count = self.tableView(tableView, numberOfRowsInSection: section)
        if count == 0 {
            return CGFloat(Double.leastNormalMagnitude)
        }
        return 44.0
    }
    

    我也希望你能把一个
    @IBOutlet
    放在一个部分并隐藏它,但遗憾的是,它似乎没有,所以

    基于这里的各种建议,我建立了以下内容,它不需要对显式大小值进行任何干扰,并且保留了您可能已经在故事板/XIB上设置的内容。它只会使要隐藏的任何部分的标题为零,行数为0(这将导致大小为
    0.0

    显然,您可以配置
    sectionshouldbehiden
    以根据需要工作;隐藏#1和#3只是任意的例子

    Swift v5

    private func sectionShouldBeHidden(_ section: Int) -> Bool {
        switch section {
        case 1, 3: return true
        default: return false
        }
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if sectionShouldBeHidden(section) {
            return nil // Show nothing for the header of hidden sections
        } else {
            return super.tableView(tableView, titleForHeaderInSection: section) // Use the default header for other sections
        }
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if sectionShouldBeHidden(section) {
            return 0 // Don't show any rows for hidden sections
        } else {
            return super.tableView(tableView, numberOfRowsInSection: section) // Use the default number of rows for other sections
        }
    }
    
    更新:不幸的是,只有当表视图的样式为
    普通时,上述内容才足够。当
    分组时
    ,每个部分之间还添加了额外的空间,这也需要注意。 这个额外的空间是节的页脚,因此可以这样处理:

    override public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if sectionShouldBeHidden(section) {
            return CGFloat.leastNormalMagnitude // Use the smallest possible value for hidden sections
        } else {
            return super.tableView(tableView, heightForFooterInSection: section) // Use the default footer height for other sections
        }
    }
    

    我在这里尝试了所有的解决方案,但都没有成功。最后,添加这些委托方法,这一个有效:


    Swift 5:

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 4 ? 0 : return super.tableView(tableView, numberOfRowsInSection: section)
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 4 ? 0.1 : super.tableView(tableView, heightForHeaderInSection: section)
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return section == 4 ? 0.1 super.tableView(tableView, heightForFooterInSection: section)
    }
    

    请注意,您需要在高度上返回0.1,但返回0不起作用。

    如果TableView是静态的(一些硬编码的行彼此不同,如设置窗口),该怎么办?从Xcode 7.3开始,Swift 2.2:作为
    TableView(uuU1;:heightForHeaderInstruction)
    的替代方法,您还可以覆盖
    TableView(U1;:titleForHeaderInstruction)
    并在要隐藏标题时返回
    nil
    。然后,截面高度将自动调整为0,以不显示任何文本。隐藏行并将行设置为高度0会导致我的约束出现问题。这个答案解决了这个问题,很好的解决方案!我有一个基于此的条件我必须隐藏一个部分我可以基于ViewWillDisplay或ViewDidDisplay中的条件隐藏tablesection吗@sasquatch@DilipTiwari是的,你可以。写一个新问题并提供一个链接。我会尽力回答的。泰克斯·泽泽。0.0也不适合我。我想知道为什么是tableView部分。不喜欢0.0。他们需要为高度指定一个值,因此0.0是不可接受的。如果要将值设置为“0”,则需要使用
    CGFloat。至少此解决方案最有效。它完全隐藏了部分,并与已建立的tableView设置配合良好!
    
    override public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if sectionShouldBeHidden(section) {
            return CGFloat.leastNormalMagnitude // Use the smallest possible value for hidden sections
        } else {
            return super.tableView(tableView, heightForFooterInSection: section) // Use the default footer height for other sections
        }
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 4 ? 0 : return super.tableView(tableView, numberOfRowsInSection: section)
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 4 ? 0.1 : super.tableView(tableView, heightForHeaderInSection: section)
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return section == 4 ? 0.1 super.tableView(tableView, heightForFooterInSection: section)
    }