从iOS7开始,在我的UITableView顶部有额外的空间它有一个UITableViewStyleGrouped样式。

这里有一个例子:

tableview从第一个箭头开始,有35个像素的无法解释的填充,然后绿色的头是一个由viewForHeaderInSection返回的UIView(其中section为0)。

有人能解释一下这个35像素的数量是从哪里来的吗?我如何才能在不切换到UITableViewStylePlain的情况下摆脱它?


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

下面的操作(Swift)解决了这个问题,但当你不需要头文件的时候,这是有效的。

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

如果你这样做,你将不得不放弃第一部分,并使用其他内容。

UITableViewDataSource实施:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return <number_of_data_sections>+1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // the first section we don't use for data
    if section == 0 {
        return 0
    }

    // starting from 1, there are sections we use
    if section == 1 { 
        let dataSection = section - 1

        // use dataSection for your content (useful, when data provided by fetched result controller). For example:
       if let sectionInfo = myFRC!.sections![dataSection] as? NSFetchedResultsSectionInfo {
            return sectionInfo.numberOfObjects
        }
    }

    return 0
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let dataIndexPath = NSIndexPath(forRow: indexPath.row, inSection: (indexPath.section - 1) )
    // return cell using transformed dataIndexPath
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 1 {
        // return your header height
    }
    return CGFloat.min
}


func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 1 {
        // return your header view
    }
    return nil
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    // in my case, even when 1st section header was of zero heigh, I saw the space, an that was a footer. I did not need footer at all, so always gave zero height
    return CGFloat.min
}

就是这样。模型不知道任何变化,因为我们在访问数据时转换了节号。

其他回答

set
  override func viewDidLoad() {
  self.tableView.delegate = self
  self.tableView.dataSource = self
}

对我来说,我尝试了所有的解决方案,但我在考虑为UITableView部分设置高度,它为我工作。(使用斯威夫特)

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

如果我添加一个高度很小的tableHeaderView,它就能解决这个问题

    let v = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.000000001))
    tableViewDocuments.tableHeaderView = v

在我的例子中,在Storyboard的原型可视化中有一个不需要的名为HeaderView的单元格,当我删除它时,一切都很好。

2019答:

你只要这样做

tableView.contentInsetAdjustmentBehavior = .never

奇怪微妙的gotchya ->

现在表视图有一个非常奇怪的行为:

在有缺口(XR等)的设备上,它不会告诉你添加更多的插入,但只有当表格从屏幕的物理顶部开始时才会这样做。 如果你不是从屏幕顶部开始,它不会那样做,但是 这两种情况都是>>与safeAreaInsets .......无关这很让人困惑

所有这些都是完全没有记录的……你可以浪费几个小时来解决这个问题。

如果你确实需要从屏幕/表格的顶部开始测量,

事实上,简单地说:

tableView.contentInsetAdjustmentBehavior = .never

一个很好的例子是,当你在一个表格的顶部添加一些横幅或类似的东西时,这是现在很常见的,你只需要将表格的顶部插入设置为你的横幅/等在运行时的任何高度。

要做到这一点,必须使用

tableView.contentInsetAdjustmentBehavior = .never

电话:/

奖金陷阱

不要忘记,现在几乎总是动态加载一些信息(用户图片、描述等等),所以在信息到达之前不能将这些值设置为最终需要的值。另一个gotchya。:/

所以你会有这样的代码:

func setTableOffsetOnceFlagAreaSizeIsKnown() {
    tableView.contentInset.top = yourSpecialFlagViewUpTop.bounds.height
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    setTableOffsetOnceFlagAreaSizeIsKnown()
}