【问题标题】:Autosizing UITableView inside custom UIView for section在自定义 UIView 中为部分自动调整 UITableView
【发布时间】:2017-02-03 07:46:59
【问题描述】:

我创建了 UIViewController 并在其上添加了 UITableView(通过自动布局固定到所有四个边缘)。

然后我设置 estimatedRowHeight (44) 和 rowHeight (UITableViewAutomaticDimension) 并返回 5 个自定义单元格。它奏效了。

现在,我想添加具有动态高度的自定义 UITableViewHeaderFooterView

接下来我会做:

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

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return R.nib.orderStatusHeaderView.firstView(owner: self)!
}

我的 OrderStatusHeaderView 是一个 xib 视图,其 UITableView 使用自动布局固定到所有 4 个边缘。

OrderStatusHeaderView:

final class OrderStatusHeaderView: UITableViewHeaderFooterView {
    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.dataSource = self
            tableView.delegate = self
            tableView.rowHeight = 44.0
        }
    }
}
extension OrderStatusHeaderView: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "\(indexPath.row)")
        cell.textLabel?.text = "\(indexPath.row)"
        cell.backgroundColor = .red
        return cell
    }
}

这显示如下:

当我点击或滚动时,所有红色单元格都会消失。会是什么呢?以及如何使UITableView 动态加载内容和UITableViewHeaderFooterView 将自行调整大小以适应UITableView.contentSize。有可能吗?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    退房:

    tableView(_:estimatedHeightForFooterInSection:)

    tableView(_:heightForFooterInSection:)

    你也有 headerInSection 的等价物。

    【讨论】:

    • 是的,看起来很棒。我已经把它展示出来了。但问题是:在tableView(_:heightForFooterInSection:) 我返回header.tableView.contentSize.height。但内容尚未计算,它使用header.tableView 估计行高并将整体contentSize.height 计算为numberOfRows * estimatedRowHeight。但是行的实际大小是UITableViewAutomaticDimensions
    • @OlexiyPyvovarov 这个方法的重点是知道要设置什么为“contentSize”高度(单元格高度),所以要求它设置contentSize高度为高度当然返回0。如果你想要“自动调整大小”,您需要使用 UITableViewAutomaticDimensions 或在方法中手动计算高度并将其返回。
    • 我想,由于行尚未显示,因此未计算大小。但是我手动创建了一个方法来计算具有动态行高的整个内容大小并且它工作了谢谢!
    • @OlexiyPyvovarov 太棒了!我建议你保留它,在我看来,自动标注方法不是那么精确或有效,也许对某些东西有用,不知道。
    【解决方案2】:

    由于您要求的是表格的页眉和页脚视图,因此您可以跳过您描述的委托方法。这些(顾名思义)用于 SECTION 页眉和页脚。

    当您设置使用 AutoLayout 作为表格页眉或页脚的视图时,其框架的高度仍然为零(这就是为什么此类视图中的按钮无法工作的原因,例如,因为它们没有接收到触摸)。

    要使用 AutoLayout 正确调整表格的页眉或页脚视图的大小,您必须自己实际计算高度,然后再次设置 headerView。在许多类似的帖子中都有详细描述:

    https://stackoverflow.com/a/28102157/756039 https://gist.github.com/marcoarment/1105553afba6b4900c10 http://collindonnell.com/2015/09/29/dynamically-sized-table-view-header-or-footer-using-auto-layout/ http://roadfiresoftware.com/2015/05/how-to-size-a-table-header-view-using-auto-layout-in-interface-builder/

    希望这会有所帮助。

    【讨论】:

    • 哦,我有点困惑 :) 我需要部分标题,而不是整个表格视图标题。答案仍然适用吗?
    • 那么快速搜索就会出现这个例如:stackoverflow.com/a/29763200/756039
    • @OlexiyPyvovarov 你没有看到我关于如何在部分中设置页眉和页脚高度的答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多