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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

对于ios7,如果你在视图控制器中分配tableview,你可以查看

self.edgesForExtendedLayout = UIRectEdgeNone;

你的问题似乎和我的相似

更新:

iOS 9.x中的Swift:

self.edgesForExtendedLayout = UIRectEdge.None

Swift 3:

self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)

其他回答

唯一对我有用的是:

迅速:

tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0

objective - c:

self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = 0;

另外,我还有多余的空间来写第一部分。这是因为我错误地使用了tableHeaderView属性。修正了,以及添加:

self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 0.01))

Swift 4代码: 对于没有section header的tableview,你可以添加以下代码:

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

你会得到标题间距为0。

如果你想要一个特定高度的头文件,传递该值:

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

和viewForHeaderinSection委托的视图。

我得到了以下的帮助:

YouStoryboard。>属性检查>取消勾选-调整滚动视图嵌入。

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

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

这对我很管用。也可以根据section给出标题的高度。