UITableView标头与其单元格的内容重叠

问题描述 投票:0回答:1

我正在使用UITableViewController视图,该视图在UITableView标头中具有许多不同高度的内容。整个标题的高度要求会有所不同,因此我需要根据其中显示的内容动态设置它。

以下是我在寻找潜在解决方案后一直在尝试的方法。 UITableView.automaticDimension似乎没有任何作用。将tableView.sectionHeaderHeight设置为静态数字是可行的,但我不能完全将其动态设置。

全部通过编程方式完成。

我要采用正确的方法吗?基本上在所有子视图都添加到headerView之后,我要设置整个标题的高度。

ViewDidLoad:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self as! UITableViewDataSource
        tableView.sectionHeaderHeight = UITableView.automaticDimension
        tableView.estimatedSectionHeaderHeight = 200;
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

应该设置标题高度吗?

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return UITableView.automaticDimension
    }
    override func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
        return 200
    }

添加标题内容:

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = headerView
        v.translatesAutoresizingMaskIntoConstraints = false
        v.addSubview(headingLabel)
        v.addSubview(subheadingLabel)
        v.addSubview(additionalContent)
        headerConstraints()
        return v
    }

这是我看到的:This is what I see

这是我希望看到的:This is what I'm hoping to see

有什么建议吗?

谢谢!

ios swift uitableview
1个回答
0
投票

我相信如果将tableView的clipsToBounds设置为true,则可以解决。

使用代码:

tableView.clipsToBounds = true

使用界面构建器:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.