UIcollectionViewCell 内 UITableView 的动态高度 [英] Dynamic height of UITableView inside UIcollectionViewCell

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

问题描述

我有一个 UICollectionViewCell,里面有一个 UTableView.我想根据其中的内容动态计算 UITableView 的高度.这意味着,UITableView 不应滚动,而应根据其内容增加/减少其高度.

I have a UICollectionViewCell which has a UTableView inside it. I want to calculate the height of UITableView dynamically based on the content inside it. It means, the UITableView shouldn't scroll but should increase/decrease its height according to its content.

推荐答案

我去过那里.有多种方法可以做到这一点,特别是如果您的行确实具有恒定的高度,那应该很容易.将行数乘以恒定高度,瞧,您就拥有了 tableView 高度.

I've been there. There are multiple ways to do that, especially if your rows really have constant height, that should be easy. Multiply the number of rows to the constant height, voila, you have your tableView height.

但是,如果您有位于 collectionViewCell 或 tableViewCell 内的 tableView 的动态单元格高度(它们是相同的),那么您需要另一种方法.

HOWEVER, if you have dynamic cell height of the tableView that is inside the collectionViewCell or tableViewCell (they're the same), then you need another approach.

我的方法是观察 keyPath contentSize.这个是完美的,我一直在我的主要生产项目中使用它.这是我使用的完整代码块,包括注释/文档;)

My approach to that is observing the keyPath contentSize. This one is perfect, I've been using this in my main production project. Here's a full block of the code that I use, including the comments/documentation ;)

/**
 Important Notes, as of 12/18/2018, 9:41PM, a eureka moment:
 - No need for label height.
 - Needs a reference for tableViewHeight.
 - After observing the newSize data, update the constraint's offset of the tableViewHeight reference.
 - And then let know the controller to not reload the data of the tableView but rather begin and end updates only.
 - beginUpdate() and endUpdate() lets the tableView to update the layout without calling the cellForRow, meaning without calling the setupCell method.
*/
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if let obj = object as? LLFTableView, obj.tag == 444 {
        if obj == self.tableView && keyPath == "contentSize" {
            if let newSize = change?[NSKeyValueChangeKey.newKey] as? CGSize {
                // Edit heightOfTableViewConstraint's constant to update height of table view
                llfPrint("New Size of the tableView: \(newSize) ✅✅✅✅✅")
                DispatchQueue.main.async {
                    self.constraint_TableViewHeight?.update(offset: newSize.height)
                    self.delegate?.reloadData()
                }
            }
        }
    }
}

那么,在实现这种 reloadData 委托方法的控制器或视图模型中会发生什么?它调用另一个委托方法来让控制器(持有超级 tableView 的)知道我们正在更新单元格的高度.

So, what happens in the controller or viewModel that implements such reloadData delegate method? It calls another delegate method to just let know the controller (That holds the super tableView) that we're updating the height of the cell.

self.tableView.beginUpdates()
self.tableView.endUpdates()

就是这样!:) 我希望这会有所帮助!

That's it! :) I hope this helps!

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

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