Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 11中隐藏节标题?_Ios_Swift_Uitableview - Fatal编程技术网

如何在iOS 11中隐藏节标题?

如何在iOS 11中隐藏节标题?,ios,swift,uitableview,Ios,Swift,Uitableview,在iOS 11中,无论项目是否为0或更多,“我的分区”标题始终显示 在iOS 10设备上,当项目计数为0时,我的代码工作,部分消失。但在iOS 11上,相同的代码没有影响 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { if sections[section].items.count > 0{ return sections[

在iOS 11中,无论项目是否为0或更多,“我的分区”标题始终显示

在iOS 10设备上,当项目计数为0时,我的代码工作,部分消失。但在iOS 11上,相同的代码没有影响

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if sections[section].items.count > 0{
        return sections[section].title
    }else{
        return nil
    }
}

在iOS 11中,如果只实现标题forheaderinsection并返回nil,则不会看到标题视图。但是,如果您还实现了
viewForHeaderInSection
,无论返回什么,都会有一个节

仅此项不会显示节标题:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return nil
}
这将显示没有标题的节标题:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return nil
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return nil
}
因此,这两个方法都可能返回nil,并且头将可见。如果只实现了HeaderInSection的标题,则不会显示标题。这似乎只有在iOS 11中才会出现。不确定这是一个bug还是一种迫使开发人员从两种方法中选择一种的方法。但是文件确认了标题标题部分的这种行为:

返回值:用作节标题的字符串。如果返回nil,则节将没有标题


因此,与显示或不显示无关,此方法只返回标题的字符串。这是有道理的。但是看起来像一个bug的是,在
viewForHeaderInSection
中返回nil将显示节头。

要隐藏节头,例如节0,请执行如下方法:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if (section == 0) {
        return CGFloat.leastNormalMagnitude //Now section 0's header is hidden regardless of the new behaviour in iOS11.
    }

    return tableView.sectionHeaderHeight
}
此解决方案也适用于分组UITableView,如下所述

更新:如果执行重新加载节(..),此解决方案会导致

N类型为“CALayerInvalidGeometry”的异常

但是,如果在
If
语句中
返回0
,则不会发生此崩溃!:)

因此,我认为我找到的最佳解决方案(至少对于普通
uitableview
)是:

实现
tableView(uu:heightForHeaderInSection:)
以返回
uitableview自动标注


titleForHeaderInSection
返回
nil
(否则它将使用表中的默认页眉高度)时,这将抑制节标题。

如果明确告诉iOS 11在
heightForHeaderInSection
中使用0的高度,它将隐藏节标题。对于非零高度的页眉,您仍然可以通过返回
UITableViewAutomaticDimension
来使用自动页眉大小调整。以下是一个解决iOS 11缺陷的解决方案示例:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  guard shouldShowSectionHeader(section: section) else { return 0 }
  return UITableViewAutomaticDimension
}

您需要实现
shouldShowSectionHeader
,以确定是否显示节头。

iOS 11.3,在生产环境中工作

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    return sections[section].headerViewModel?.makeView(bindImmediately: true)
}

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

    return sections[section].headerViewModel == nil ? 0 : UITableViewAutomaticDimension
}

从标题部分的
视图返回空视图
swift 3、4和4.2

隐藏tableView标题的步骤

tableView.tableHeaderView?.frame = CGRect.zero
然后把它拿回来

tableView.tableHeaderView?.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 66)
对我有用的是:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0.01
}

您是否可能实现heightForHeaderInSection或viewForHeaderInSection?这可能会使您的分区显示或不显示。苹果的文档也提到了TitleForHeaderInstruction“返回值:一个用作节标题的字符串。如果返回nil,则节将没有标题。”并没有说将没有节,只有标题。哇。这似乎是一个主要错误。它看起来更像一个错误,而不是一个功能:)返回0会导致UITableView使用默认值。这是未记录的行为。如果你返回一个非常小的数字,你实际上会得到一个零高度的标题。这不是我的经验。我的经验是,返回0会抑制标题。在最新版本的iOS中,它可能已更改,但这在过去肯定是一个问题。它还可能取决于是否使用分组UITableView。好的,我绝对不是在说分组表视图。这是一种常见的类型,所以它可能与其他类型相关。
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0.01
}