【问题标题】:Extra space at section header in UITableView with Autolayout [duplicate]带有自动布局的 UITableView 中部分标题的额外空间 [重复]
【发布时间】:2022-01-05 07:04:39
【问题描述】:

我有一个 UITableViewController 有:

  1. tableHeaderView 的 UIView
  2. sectionHeaderView 的 UIView
  3. UITableViewCell 用于 tableview 的单元格
  4. tableFooterView 的 UIView

我的故事板中有 UITableViewController,也定义了 tableHeaderView 和 tableFooterView。 sectionHeaderView 和 tableviewcell 在 nib 文件中是分开的。

override func viewDidLoad() {
    super.viewDidLoad()
    
    //Register the different cells of the tableview
    // First load table nib
    let bundle = Bundle(for: type(of: self))

    // Register table cell class from nib
    let cellNib = UINib(nibName: "CheckingDocumentCheckCell", bundle: bundle)
    tableView.register(cellNib, forCellReuseIdentifier: CheckingDocumentCheckCellVC.reuseIdentifer)
    
    //Register table section header
    let headerCellNib = UINib(nibName: "CheckingDocumentCategoryHeader", bundle: bundle)
    tableView.register(headerCellNib, forCellReuseIdentifier: CheckingDocumentCategoryHeaderCellVC.reuseIdentifer)

    
    let tapGestureReconizer = UITapGestureRecognizer(target: self, action: #selector(tap))
    tapGestureReconizer.cancelsTouchesInView = false
    tableView.addGestureRecognizer(tapGestureReconizer)
    
    tableView.keyboardDismissMode = .onDrag
    
    
    tableView.tableHeaderView = containerHeaderView
    tableView.tableFooterView = containerFooterView
    
    saveCheckingDocumentButton.backgroundColor = UIColor(hexString: ConstantsEnum.lideraColor)
    saveCheckingDocumentButton.tintColor = UIColor.white
    saveCheckingDocumentButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
    saveCheckingDocumentButton.layer.cornerRadius = 5
    saveCheckingDocumentButton.layer.borderWidth = 1
    saveCheckingDocumentButton.layer.borderColor = UIColor(hexString: ConstantsEnum.lideraColor)?.cgColor
    saveCheckingDocumentButton.setTitle(NSLocalizedString("SAVECHECKINGDOCUMENT", comment: ""), for: .normal)
    
}

我的页眉视图、页脚视图和表格单元工作正常。我遇到的问题是节标题在每个节中留下一个空白,我无法重现问题所在(红色框是我要删除的空格):

我对部分标题的看法是:

我实现视图的方法是:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let category = myCheckingDocumentResponse?.document?.categoriesQuestions[section]
    let cell = tableView.dequeueReusableCell(withIdentifier: CheckingDocumentCategoryHeaderCellVC.reuseIdentifer) as! CheckingDocumentCategoryHeaderCellVC
    cell.headerContainer.backgroundColor = .lightGray
    cell.title.text = String(format: "%@\n%@", category?.description ?? "", category?.questionDescription ?? "")
    return cell
}

如果我关闭此方法并且不使用不显示空白的节头,那么需要与该节头相关的东西。

【问题讨论】:

    标签: swift uitableview autolayout uitableviewsectionheader


    【解决方案1】:

    我今天刚刚遇到这个问题,终于找到了一个有意义且有效的答案:

    self.tableView.sectionHeaderTopPadding = 0
    

    看起来这个属性是刚刚在 iOS 15 中添加的

    https://stackoverflow.com/a/70196640/7798489

    【讨论】:

    • 感谢@rpecka 工作正常,因为我在模拟器中使用 iOS 15!。