ios - TableView 位于另一个 TableView 的标题内

标签 ios swift uitableview tableview swift4

我有一个带有标题的 TableView (1)。我想在该标题中放置另一个 TableView (2)。问题是第二个 TableView 的单元格是动态的,因此我需要确保第一个 TableView 的标题也是动态的。

我已按照本教程将第一个 TableView 的标题设为动态:

https://useyourloaf.com/blog/variable-height-table-view-header/

但是当我运行模拟器时, TableView 标题甚至不显示,我相信这与第二个 TableView 的单元格是动态的这一事实有关,因此没有为第一个 TableView 的标题分配高度.

所以我的问题是,是什么导致标题不显示以及如何修复它?

这是我的代码:

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var firstTableView: UITableView!
    @IBOutlet weak var headerView: UIView!
    @IBOutlet weak var secondTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        firstTableView.delegate = self
        firstTableView.dataSource = self

        secondTableView.delegate = self
        secondTableView.dataSource = self

        // Make the first table view's cells dynamic
        firstTableView.rowHeight = UITableView.automaticDimension
        firstTableView.estimatedRowHeight = 50

        // Make the second table view's cells dynamic
        secondTableView.rowHeight = UITableView.automaticDimension
        secondTableView.estimatedRowHeight = 50

        // Register the custom xib for the first table view
        firstTableView.register(UINib(nibName: "FirstTableViewCell", bundle: nil), forCellReuseIdentifier: "FirstTableViewCell")

        // Register the custom xib for the second table view
        secondTableView.register(UINib(nibName: "SecondTableViewCell", bundle: nil), forCellReuseIdentifier: "SecondTableViewCell")
    }

    /**
     Used to resize the table view header.
     Source: https://useyourloaf.com/blog/variable-height-table-view-header/
     */
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        guard let headerView = firstTableView.tableHeaderView else {
            return
        }

        let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)

        if (headerView.frame.size.height != size.height) {
            headerView.frame.size.height = size.height
            firstTableView.tableHeaderView = headerView
            firstTableView.layoutIfNeeded()
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.firstTableView {
            return 100
        }

        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == self.firstTableView {
            // Logic to create new cell
        } else if tableView == self.secondTableView {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "SecondTableViewCell", for: indexPath) as? SecondTableViewCell  else {
                fatalError("The dequeued cell is not an instance of SecondTableViewCell.")
            }

            cell.initialize()

            return cell
        }

        return UITableViewCell()
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

}

最佳答案

如果您希望 TableView 上方的 View 并且所有 View 一起滚动,一个好的实现是将所有这些 View 嵌入 ScrollView 中,禁用 TableView 滚动并给它一个高度约束,然后更改运行时的高度约束取决于表项,以将表扩展到最大可能高度( How? )。这样你就会得到预期的行为。

关于ios - TableView 位于另一个 TableView 的标题内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57015089/

相关文章:

ios - View Controller 的键命令在模态显示时包含在内

swift - 当我单击主页上的一行时,转到下一页,但不显示上下文

ios - 是否可以在照片中搜索对象?

iphone - 自定义单元格无法在 ios 6 的表格 View 中显示

ios - dismissViewControllerAnimated 仅在 iOS 7.1 中导致 EXC_BAD_ACCESS

ios - 启动 NSManagedObjectContext 会产生 fatal error ?

arrays - 用一个条件过滤多个数组

ios - 来自 UITableView 的 View 消失

ios - 如果我在 TableView 中修改一行,那么它也会同时修改其他一些单元格。 (SWIFT编程)

ios - 将 UIView 从一个选项卡移动到另一个选项卡