【问题标题】:Swift - Non-scrollable UITableViewController inside UITableViewControllerSwift - UITableViewController 内的不可滚动的 UITableViewController
【发布时间】:2019-12-07 07:00:36
【问题描述】:

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

为了显示这一点,我有一个 UITableViewController 子类,它以帖子作为标题,而 cmets 在单元格中。

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

这需要将 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):我的界面完全是代码;没有与这种情况相关的情节提要

注意(2): ProfileTableViewControllers 的内容是动态的,因此约束需要在变化时更新。

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      要让 tableview 将其高度调整为内容高度,您需要设置一个额外的高度约束并在每次数据重新加载时更新它。

      像这样声明约束,以便可以从类中的其他位置访问它。

      private lazy var heightConstraint: NSLayoutConstraint = {
          let constraint = tableView.heightAnchor.constraint(equalToConstant: 0)
          constraint.isActive = true
          return constraint
      }()
      

      并且在重新加载数据时,将约束常量值设置为tableviews内容高度

      tableView.reloadData()
      layoutIfNeeded()
      heightConstraint.constant = tableView.contentSize.height
      

      调用layoutIfNeeded 将有助于tableview 确定其内容大小

      别忘了在表格视图中关闭滚动

      tableView.isScrollEnabled = false
      

      【讨论】:

      • 不使用惰性属性意味着集合只会被调用一次?
      • 是的,我们只需要创建一次这个约束。
      • 嗯,很接近了,但是某些 tableViewCells 没有完全显示; tableViews 被掩盖了。哪些受到影响似乎很随机。
      【解决方案3】:

      在我的例子中,我通常创建一个自定义视图,其中包含 UITableView,然后覆盖 intrinsicContentSize 属性以确定视图的大小。看看下面的代码。

      override var intrinsicContentSize: CGSize {
          self.tableView.layoutIfNeeded()
          let size = self.tableView.contentSize
          print("size = \(size)")
          return size
      }
      

      当您使用 AutoLayout System 时,这种方式可能在您的代码中运行良好。

      如果你使用 UICollectionView,可以使用collectionViewContentSize 属性来代替contentSize

      【讨论】:

      • 所以我必须继承 UIViewController 而不是 UITableViewController,创建 UIView 的子类,我将它作为子视图添加到 ViewController,然后添加一个 tableView 作为它的子视图界面视图?
      猜你喜欢
      • 2012-10-02
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 2014-01-21
      • 1970-01-01
      相关资源
      最近更新 更多