【问题标题】:`UITableView` extra space at the top`UITableView` 顶部的额外空间
【发布时间】:2015-07-16 14:57:42
【问题描述】:

我不记得在我的场景中添加UITableViews 时看到这个额外的空间。这发生在我从 Interface Builder 的对象库中拖动默认对象时。

为了突出差距,我已将表格的背景颜色更改为灰色。

我使用以下代码在表格中填充数据。

extension UserListViewController: UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: nil)
        cell.textLabel?.text = users[indexPath.row].name
        return cell
    }
}

我在视图控制器的viewDidLoad 中为轮廓设置了dataSource

tableView.dataSource = self

那么这个差距从何而来?我该如何摆脱它?

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    您也可以直接从 StoryBoard 中 UIViewController 的属性检查器中取消选择它,因此您无需添加代码。

    【讨论】:

      【解决方案2】:

      您可以尝试在您的控制器中进行设置:

      self.automaticallyAdjustsScrollViewInsets = false
      

      这个属性是不言自明的。在 iOS 7 中,Apple 引入了一个半透明的顶部栏。通常他们希望您的 UITableview 从顶部开始,就像某些部分在顶部栏后面一样。在这种情况下,在顶部自动插入是很有用的,以便内容可见。

      【讨论】:

      • 宾果游戏!感谢您的补充说明。会尽快接受的。
      【解决方案3】:

      如果 tableView 在 tableViewContorller 中并且 table view 是静态的,则将 tableView 单元格的样式从“Grouped”更改为“Plain”。这将删除 tableView 单元格顶部的额外空间。

      【讨论】: