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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

如果你选择UITableViewStyleGrouped的样式,你需要实现Header和Footer高度委托方法,并且返回值需要大于0;是这样的:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

其他回答

扔我的十美分在-我有同样的问题后,我删除了一个标题单元格从表视图。为了解决这个问题,我将属性检查器中的TableView样式从“分组”更改为“普通”,并删除了额外的空间。

我喝了和arielyz一样的药。一旦我把UITableView移到不是父视图的第一个子视图,它就消失了。空间是20px,不是35。

我无法在肖像xib中重现它,只能在风景xib中重现。如果我可以在一个简单的演示应用程序中重现它,我将稍后提交一个雷达漏洞。

取消勾选“调整滚动视图嵌入”

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return CGFLOAT_MIN;
}

这就是所有的朋友们!

你可以检测你的应用是否运行iOS7或更高版本,并在你的表视图委托中添加这两个方法(通常在你的UIViewController代码中)

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

这可能不是一个优雅的解决方案,但对我来说是可行的

斯威夫特版本:

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

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