UITableViewCell 无法根据它包含的动态 CollectionView 调整高度 [英] UITableViewCell not able to adjust height based on the dynamic CollectionView it contains

查看:32
本文介绍了UITableViewCell 无法根据它包含的动态 CollectionView 调整高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我从解释视图层次结构开始.我使用的是普通的 UITableView,在 UITableViewCell 中有一个高度动态的 collectionView.

So let me start by explaining the view hierarchy. I'm using a normal UITableView, inside the UITableViewCell there is a collectionView which is dynamic in height.

我如何在 UITableView 的 willDisplay 单元格中为 UICollectionView 设置数据源和委托:

How I'm setting the datasource and delegate for UICollectionView at the willDisplay cell of UITableView:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if indexPath.section == 0 {

        guard let collectionCell = cell as? movieListingTableViewCell else { return }

        collectionCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
        collectionCell.selectionStyle = .none
    }
}

collectionView 具有顶部、底部、前导、尾部和高度约束.

The collectionView has the top, bottom, leading, trailing and height constraints.

然后我在 UITableView 单元子类中使用高度约束并将其设置为 collectionView 高度,

Then I'm using the height constraint and setting it to the collectionView height as so in the UITableView cell subclass,

override func layoutSubviews() {

    timingCollectionHeight.constant = timingCollection.collectionViewLayout.collectionViewContentSize.height
}

发生的情况是我的单元格显示不正确,因为有空格.例如,如果有一个 UITableView 单元格的 collectionView 具有很大的高度,则其他 UITableView 单元格也会以某种方式使用相同的高度.滚动表格后,单元格显示正确.

What happens is that my cells are not appearing properly as in there are blank spaces. For eg, if there is one UITableView cell which has a collectionView with a large height, other UITableView cells also somehow use the same height. After scrolling through the table the cells appear correctly.

推荐答案

Swift 示例

首先,在 tableViewCell 类中获取 collectionViewHeightConstraint 的出口,如下所示:-

First, get an outlet of your collectionViewHeightConstraint in your tableViewCell Class as shown below:-

@IBOutlet weak var collectionViewHeight: NSLayoutConstraint!

然后在您的 ViewController 类的 viewDidLoad() 方法中添加以下代码以获取 tableViewCell 的动态高度:-

Then inside viewDidLoad() method of your ViewController Class add below code for dynamic height of tableViewCell:-

myTableView.estimatedRowHeight = 100;  // Default tableViewCell height
myTableView.rowHeight = UITableViewAutomaticDimension;

然后在您的 tableView cellForRowAtindexPath 委托中添加以下代码以根据 CollectionView 高度更新 tableView Cell 的高度:-

Then inside your tableView cellForRowAtindexPath delegate add below code to update the height of tableView Cell as per CollectionView height:-

cell.frame = tableView.bounds;  // cell of myTableView
cell.layoutIfNeeded()
cell.myCollectionView.reloadData()
cell.collectionViewHeight.constant = cell.myCollectionView.collectionViewLayout.collectionViewContentSize.height;

希望这会有所帮助.快乐编码:)

Hope this helps. Happy Coding :)

这篇关于UITableViewCell 无法根据它包含的动态 CollectionView 调整高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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