我正在使用 Swift 制作一个应用程序,但遇到了一个问题: 我有一个系统,人们可以在其中发布“帖子”,并对这些“帖子”发表评论。
为了显示这一点,我有一个 UITableViewController 子类,它将帖子作为标题,并在单元格中添加注释。
现在我有一个屏幕需要列出这些帖子。理想情况下,我希望将每个帖子及其评论放在一个单元格中。
这需要将 UITableViewController 子类(我们称之为 PostTableViewController
)嵌入到另一个 UITableViewController 的单元格中。
问题是,我不希望每个 ProfileTableViewController 在其单元格内滚动。我需要关闭 tableViewController 上的滚动,并使其高度扩展以适合其整个内容。
我已经查看了 this post 上的答案,但我仍然遇到问题。目前还不清楚 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):我的界面完全是代码;没有与这种情况相关的 Storyboard
注意(2):ProfileTableViewControllers 的内容是动态的,因此约束需要在更改时更新。
最佳答案
对于那些将来关注这一问题的人,我最终所做的是将帖子分成 tableView 部分,而不是单元格,然后为 profileTableViewController 添加一个单元格,并为其下面的评论添加一个单元格。
关于ios - Swift - UITableViewController 内不可滚动的 UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57262280/