首页 > 解决方案 > Swift - UITableViewController 内的不可滚动的 UITableViewController

问题描述

我正在使用 Swift 制作应用程序,但遇到了一个问题:我有一个系统,人们可以在其中发布“帖子”,并对这些“帖子”发表评论。

为了显示这一点,我有一个 UITableViewController 子类,它以帖子作为标题,并在单元格中添加评论。

现在我有一个需要列出这些帖子的屏幕。理想情况下,我希望将每个帖子及其评论放在一个单元格中。

这需要将 UITableViewController 子类(我们称之为PostTableViewController)嵌入另一个 UITableViewController 的单元格中。

问题是,我不希望这些 ProfileTableViewController 中的每一个都在它们的单元格内滚动。我需要在 tableViewController 上关闭滚动,并使其高度扩展以适合其整个内容。

我已经查看了这篇文章的答案,但我仍然遇到问题。目前尚不清楚 tableView 是否正确调整大小,因为 ProfileTableViewController 在其各自单元格中的高度始终为 0。如果我手动设置恒定高度约束,我可以看到其中的一部分,但我希望它自动调整大小适合tableView。

我当前的约束设置是:

    //dateView is another view in my tableViewCell
    profileViewController.view.topAnchor.constraint(equalTo: dateView.bottomAnchor).isActive = true
    profileViewController.view.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    profileViewController.view.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    profileViewController.view.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true

    profileViewController.view.bottomAnchor.constraint(equalTo: profileViewController.tableView.tableFooterView!.bottomAnchor).isActive = true
    profileViewController.tableView.topAnchor.constraint(equalTo: profileViewController.view.topAnchor).isActive = true

    self.bottomAnchor.constraint(equalTo: homeViewController.view.bottomAnchor).isActive = true

那么如何让 tableView 自动适应它的内容,并设置约束让 tableViewCell 自动调整为 tableView 呢?

注意(1):我的界面完全是代码;没有与这种情况相关的故事板

注意(2): ProfileTableViewControllers 的内容是动态的,所以当它改变时约束需要更新。

标签: iosswiftuitableview

解决方案


对于那些将来会关注这个问题的人,我最终所做的是将帖子分成 tableView 部分,而不是单元格,然后为 profileTableViewController 添加一个单元格,并为它下面的评论添加一个单元格。


推荐阅读