ios - 不能使用 UITableViewAutomaticDimension 在 UITableView 中隐藏节标题

标签 ios uitableview uitableviewsectionheader

我有一个带有节标题的 UITableView。整个 tableview 为单元格和标题设置了 UITableViewAutomaticDimension :

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet var sectionHeader: MyTableViewHeaderFooterView!

    let data = [
        "Lorem ipsum dolor sit amet",
        "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation",
        "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 44.0

        self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
        self.tableView.estimatedSectionHeaderHeight = 44.0
    }

    // MARK: - Table View

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            self.sectionHeader.label.text = "Section \(section)"
            return self.sectionHeader
        } else {
            return nil
        }
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MyTableViewCell
        cell.label.text = data[indexPath.row]
        return cell
    }


}

问题是我想隐藏一些部分标题。第二部分标题应该被隐藏,因为我返回 nil 而不是 View ,但是空间仍然保留。为什么?

Screenshot of UITableView where space is reserved for an empty section header

Github 上的 Xcode 项目:
https://github.com/bvankuik/SectionHeaderAlwaysVisible

最佳答案

Apple 关于 UITableViewAutomaticDimension 的文档说:

Returning this value from tableView:heightForHeaderInSection: or tableView:heightForFooterInSection: results in a height that fits the value returned from tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: if the title is not nil.



恐怕您需要更改计算标题高度的方式,如下所示:
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 1 {
        return 0
    } else {
        return UITableViewAutomaticDimension
    }
}

关于ios - 不能使用 UITableViewAutomaticDimension 在 UITableView 中隐藏节标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36544290/

相关文章:

ios - 进行实时 HTTP 流式传输时显示 AVPlayerItem 状态未知

ios - TableViewCell 中的更改立即生效

ios - UITableViewCell 内容在滚动时合并

ios - 如何刷新 UITableView 中的单节标题文本

ios - 扩展的 UITableViewCell 无法正常工作?

ios - Express.IO 连接不断失败并显示 HTTP 500

ios - 如何使用步进值更新折线图?

ios - UI 在 tableview reloadData() 上卡住

ios - 重新加载节中的标题,而不用快速重新加载节行

ios - 单击 View 外部时如何关闭模态表单?