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

这里有一个例子:

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

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


更新(回答):

在iOS 11及更高版本中:

tableView.contentInsetAdjustmentBehavior = .never

当前回答

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

其他回答

下面的操作(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
}

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

我认为制作UIEdgeInsets -35 0 0 0是乏味的。在我的例子中,我实现了tableView: hightforheaderinsection:方法,它有可能返回0。

当我把0改成0.1f时,问题就消失了。

另一个快速评论…甚至在XCode 6.1中,UIScrollViews, UITextViews和UITableViews顶部出现垂直空格的错误。

有时,修复此问题的唯一方法是进入Storyboard并拖动问题控件,使其不再是页面上的第一个子视图。

(感谢Oded为我指明了这个方向……我发表这篇评论,只是为了添加一些截图,以演示症状和解决方法。)

确保您已经为表提供了约束。我没有遇到过类似的问题,在桌子底部有一个无法解释的填充物。

我不确定这个答案是否正确,但它对我有用

我选择表视图,导航到标尺,改变Y值为零