ios - 从 UITableView 中删除顶部填充

标签 ios swift uitableview uikit

我有一个 UITableView 作为另一个 UITableView(嵌套的 UITableView)的单元格。 有一个我无法解释的顶部填充,顶部填充仅在我使 CustomTableCell 具有 UITableView 时出现。

class CustomTableCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {

  private let cellId = "cellId"

  lazy var tableView: UITableView = {

    let tv = UITableView(frame: .zero, style: .grouped)
    tv.translatesAutoresizingMaskIntoConstraints = false
    tv.backgroundColor = .green
    tv.delegate = self
    tv.dataSource = self
    tv.alwaysBounceVertical = false
    tv.isScrollEnabled = false

    tv.separatorColor = .red;
    tv.separatorStyle = .none;

    tv.register(InnerTableCell.self, forCellReuseIdentifier: self.cellId)
    return tv
  }()

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
  }

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! InnerTableCell
    return cell
  }

  func setupAutoLayout() {

    tableView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
    tableView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
  }

  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    contentView.backgroundColor = .white
    contentView.addSubview(tableView)
    setupAutoLayout()
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

这里是整个代码的链接:https://ideone.com/QAxkPR

这是它在 reveal 中的样子 enter image description here

最佳答案

将样式更改为 .plain 而不是 .grouped

let tv = UITableView(frame: .zero, style: .plain)

关于ios - 从 UITableView 中删除顶部填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48632214/

相关文章:

ios - 检查照片中是否存在图像

ios - 发送到实例的 CoreData 无法识别的选择器

ios - CellForItemAt : index path not updating variable

ios - UITableViewCell 从 1 计数到 50

ios - 快速定位中心 UICollectionViewCell

ios - Overflow-y 不适用于 iPad

ios - UITableView 滚动后 UITableViewCell 自定义按钮图像显示不正确

ios - NSFetchedResultsController 在向第二部分插入行时在第一部分返回错误的行数

UITableView 旋转问题

ios - 从 Google speech api 获取 "transcript"值