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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

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

    return CGFLOAT_MIN;
}

这就是所有的朋友们!

其他回答

当使用分组TableView时,使用这个来避免viewWillAppear中的边界切割

self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
override func viewWillAppear(animated: Bool) {
        self.edgesForExtendedLayout = UIRectEdge.None

 //  OR

self.sampleTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);

   //OR

 self.automaticallyAdjustsScrollViewInsets = false
        }

这是使用Swift 3的iOS 10的解决方案:

你可以通过实现UITableViewDelegate中的以下方法来去除顶部和底部填充。

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

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

    return CGFLOAT_MIN;
}

这就是所有的朋友们!

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