UITableView标头在运行时的动态高度

Sta*_*nov 5 uitableview uiscrollview ios

我知道有很多关于它的帖子,但也许在最新的iOS上有一些更新...

我认为我们所有人都有一项创建任务,viewController其中包含大量内容,其中大部分内容都是自我调整的,而最底层则表明你需要展示一些tableView包含很多内容的内容......

可以做的第一个解决方案是使用UIScrollView,而根本不关心reusableCells.
所述第二是使用UITableViewheaderView并手动调整其高度(或通过调用systemLayoutSizeFittingSize:在需要时)各一次.
也许第三解决方案是使用UITableView和自我尺寸UIView分别与具有UIEdgeInsetstableView.并根据什么物体上具有较高的"zIndex的",它可以带来与处理的交互问题......
规定解决方案是使用电池上面全部内容,就像一个独立的单元.根本不确定这是个好主意......

问题:这个问题有新的解决方案吗?我没有深入研究它2年......也许在新的iOS中有类似reusableViews的东西UIScrollView...当然,目标是拥有可重复使用的单元格,并使用自动布局标题而无需手动更新其高度...

Sou*_*rma 11

复制自useyourloaf.com

雨燕5.0

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    guard let headerView = tableView.tableHeaderView else {return}
    let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
    if headerView.frame.size.height != size.height {
        headerView.frame.size.height = size.height
        tableView.tableHeaderView = headerView
        tableView.layoutIfNeeded()
    }
}
Run Code Online (Sandbox Code Playgroud)


jo.*_*.On 9

我喜欢这里的做法:

• 如果您想根据内容动态设置 tableview 标题高度,只需self.tableView.layoutTableFooterView()在将 headerView 设置为 TableViewHeader 后立即调用(在 之后self.tableView.tableHeaderView = view

• 如果您需要在运行时更新您的 tableview 标题高度,请self.tableView.layoutTableFooterView()在您更新 tableview 标题的值后立即调用。

(这显然也适用于 tableviewFooters,但不能用于部分页眉/页脚)

extension UITableView {

    //Variable-height UITableView tableHeaderView with autolayout
    func layoutTableHeaderView() {

        guard let headerView = self.tableHeaderView else { return }
        headerView.translatesAutoresizingMaskIntoConstraints = false

        let headerWidth = headerView.bounds.size.width
        let temporaryWidthConstraint = headerView.widthAnchor.constraint(equalToConstant: headerWidth)

        headerView.addConstraint(temporaryWidthConstraint)

        headerView.setNeedsLayout()
        headerView.layoutIfNeeded()

        let headerSize = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
        let height = headerSize.height
        var frame = headerView.frame

        frame.size.height = height
        headerView.frame = frame

        self.tableHeaderView = headerView

        headerView.removeConstraint(temporaryWidthConstraint)
        headerView.translatesAutoresizingMaskIntoConstraints = true
    }

}
Run Code Online (Sandbox Code Playgroud)

来源


小智 9

我正在使用 XCode 10.3,这是我的解决方案,使用表头视图与您的第二个解决方案一起使用。

首先,您将创建一个带有 xib 文件的分离视图,例如内部带有标签。并将此标签的约束(顶部、左侧、底部、右侧)应用到单元格的容器视图。并设置numberOfLines= 0。
示例图像
awakeFromNib()在您的视图类中更新您的函数。

override func awakeFromNib() {
    super.awakeFromNib()
    ourLabel.translatesAutoresizingMaskIntoConstraints = false
}
Run Code Online (Sandbox Code Playgroud)

其次,在您的 viewController 上,设置您的 tableView:

tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 64
Run Code Online (Sandbox Code Playgroud)

记住不要委托这个方法:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
Run Code Online (Sandbox Code Playgroud)

因为我们已经设置了视图的约束。

最后,在委托方法上返回它

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Run Code Online (Sandbox Code Playgroud)

标题的视图。

let view = UINib(nibName: String(describing: SimpleHeaderTitleView.self), bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! SimpleHeaderTitleView
 view.ourLabel.text = "Your longgggg text"
 return view
Run Code Online (Sandbox Code Playgroud)



完毕!检查它是否有效。


Ris*_*hab 8

我猜你在这里谈论表格视图的节标题.如果是这样,你绝对可以使用自动布局的节标题.

使用以下两个代码 viewDidLoad:

tableView.sectionHeaderHeight = UITableViewAutomaticDimension
tableView.estimatedSectionHeaderHeight = 36;
Run Code Online (Sandbox Code Playgroud)

现在viewForHeaderInSection:尝试下面的代码,只是为了了解事情是如何运作的.根据您的要求进行更改.

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label: UILabel = {
       let lb = UILabel()
        lb.translatesAutoresizingMaskIntoConstraints = false
        lb.text = "HEADER \(section) with a loooooooooooooooonnngngngngngngngng texxxxxxxxxxxxxxxxt"
        lb.textColor = .black
        lb.backgroundColor = .yellow
        lb.numberOfLines = 0
        return lb
    }()

    let header: UIView = {
        let hd = UIView()
        hd.backgroundColor = .blue
        hd.addSubview(label)
        label.leadingAnchor.constraint(equalTo: hd.leadingAnchor, constant: 8).isActive = true
        label.topAnchor.constraint(equalTo: hd.topAnchor, constant: 8).isActive = true
        label.trailingAnchor.constraint(equalTo: hd.trailingAnchor, constant: -8).isActive = true
        label.bottomAnchor.constraint(equalTo: hd.bottomAnchor, constant: -8).isActive = true
        return hd
    }()
    return header
}
Run Code Online (Sandbox Code Playgroud)


小智 2

这就是我的处理方法

使用 tableview 我已经为 XIB 中的标题创建了 UI

现在在下面的委托方法中

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

}
Run Code Online (Sandbox Code Playgroud)

我为标题创建一个 UIView 并根据内容计算高度并返回相同的高度。

现在我可以从以下委托方法返回相同的标题视图

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

}
Run Code Online (Sandbox Code Playgroud)

根据本节,我再次从 xib 创建一个视图,并从该方法返回该视图。

就我而言,我只需要一个表的标题视图,因此我保留了 2 个部分并返回了第一部分的标题视图。