UITableViewCell 内的 UICollectionView 不能正确动态调整大小 [英] UICollectionView inside UITableViewCell does NOT dynamically size correctly

查看:31
本文介绍了UITableViewCell 内的 UICollectionView 不能正确动态调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableViewCell 中有一个 UICollectionView.

I have a UICollectionView inside a UITableViewCell.

我将 UITableViewCell 行高设置为动态:

I am setting the UITableViewCell row height to be dynamic:

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

我的 UICollectionView 有可变数量的项目:

My UICollectionView has a variable number of items:

func collectionView(_ collectionView: UICollectionView,
                    numberOfItemsInSection section: Int) -> Int {
    return items.count
}

现在是让我头疼的一系列事件:

Now here is the sequence of events that is causing me headache:

  • UITableView 调用委托 cellForRowAt 并将具有动态高度的 UITableViewCell 出列
  • 在 NEXT 循环中,UITableViewCell 中的 UICollectionView 调用其委托 numberOfItemsInSection 和 cellForItemAt 来确定 UICollectionView contentSize.
  • 现在为时已晚,因为 UITableViewCell 已经使用没有单元格的 UICollectionView 动态调整大小,因此 UITablewViewCell 高度太短,不适合 UICollectionView.

我通过重新加载 UITableView 发现了一个丑陋的解决方法,但我根本不喜欢这种方法:

I have found an ugly work around by reloading the UITableView but I don't like this approach at all:

private func reloadCell() {
    let when = DispatchTime.now()
    DispatchQueue.main.asyncAfter(deadline: when) {
        if let indexPath = self.chatViewController?.chatTableView.indexPath(for: self) {
            self.tableView.reloadRows(at: [indexPath], with: .none)
        }
    }
}

我的问题是我是否可以强制 UICollectionView 重新加载并立即调用它的委托,以便自动布局引擎可以正确计算 UITableViewCell 高度?

My question is if I can force the UICollectionView to reload and call it's delegates immediately so that the UITableViewCell height can be computed by the autolayout engine correctly?

推荐答案

在viewDidLoad中,为collection view添加观察者,

In viewDidLoad, add observer for collection view,

 collectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.Old.union(NSKeyValueObservingOptions.New), context: self)

现在,重写器观察 keypath 的值,用于通过 KVO 控制集合视图重新加载完成处理程序.

Now, overrider observe value for keypath for controlling collection view reload completion handler through KVO.

 override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) 
 {
    // You will get here when the reloadData finished 
 }

现在,在视图中将消失删除为集合视图设置的观察者.

Now, in view Will Disappear remove observer that was set for collection view.

 override func viewWillDisappear(_ animated: Bool) {
 {
   // remove collection view observer   
 }

相同的逻辑将应用于 tableview,以获得包含集合视图的完全可见的 tableview 单元格.

Same logic will be applied for tableview in order to get fully visible tableview cell containing collection view inside it.

这篇关于UITableViewCell 内的 UICollectionView 不能正确动态调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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