基于动态集合视图的 UITableView 的动态高度 [英] Dynamic height for a UITableView based on a dynamic collection view

查看:22
本文介绍了基于动态集合视图的 UITableView 的动态高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在 UITableViewCell 中添加一个 UICollectionView.collectionView 可以有不同数量的项目.所以 collectionView 应该在 tableView 内适当调整.

我已经在我的项目中实现了这个:

现在,在您的 tableViewCell 类中创建一个 NSKeyValueObserver 并将其添加到 UICollectionViewcontentSize 属性和将 contentSize 更改分配给 collectionView 的高度约束.

class FormCollectionTableViewCell: UITableViewCell{var collectionViewObserver:NSKeyValueObservation?覆盖 funcawakeFromNib() {super.awakeFromNib()添加观察者()}覆盖 func layoutSubviews() {super.layoutSubviews()layoutIfNeeded()}func addObserver() {collectionViewObserver = collectionView.observe(\.contentSize, changeHandler: { [weak self] (collectionView, change) inself?.collectionView.invalidateIntrinsicContentSize()self?.collectionViewHrightConstarint.constant = collectionView.contentSize.heightself?.layoutIfNeeded()})}去初始化{collectionViewObserver = nil}}

并在您的 viewController 类中使用以下代码:

override func viewDidLayoutSubviews() {super.viewDidLayoutSubviews()tableView.reloadData()}

现在,您的表格视图将具有动态高度的集合视图.不要忘记禁用 UICollectionView 的滚动并调整它的流程.

I have to add a UICollectionView inside a UITableViewCell. The collectionView can have a different number of items. So the collectionView should adjust properly inside the tableView.

I have implemented this inside my Project: https://github.com/vishalwaka/DynamicCollectionViewInsideTableViewCell

In my case, the height of the cell is not being the adjusted after the height of the collectionView is set.

How can I set the collectionView to not be scrollable and also show all content inside the tableviewcell.

解决方案

Create a collectionView inside UITableViewCell with Leading, Trailing, Top, Bottom, Height constraint like this:

Now, in your tableViewCell class make a NSKeyValueObserver and add this on UICollectionView's contentSize property and assign contentSize changes to collectionView's height constraint.

class FormCollectionTableViewCell: UITableViewCell{

  var collectionViewObserver: NSKeyValueObservation?

  override func awakeFromNib() {
    super.awakeFromNib()
    addObserver()
  }

  override func layoutSubviews() {
        super.layoutSubviews()
        layoutIfNeeded()
  }

  func addObserver() {
       collectionViewObserver = collectionView.observe(\.contentSize, changeHandler: { [weak self] (collectionView, change) in
            self?.collectionView.invalidateIntrinsicContentSize()
            self?.collectionViewHrightConstarint.constant = collectionView.contentSize.height
            self?.layoutIfNeeded()
        })
    }
   deinit {
      collectionViewObserver = nil
   }
}

And in your viewController class use the following code:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.reloadData()
}

Now, your table view will have collection view with dynamic height. Don't forgot to disable scrolling of UICollectionView and adjusting it's flow.

这篇关于基于动态集合视图的 UITableView 的动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆