自动调整包含UICollectionView的UITableViewCell的大小 [英] Auto-sizing UITableViewCell which contains UICollectionView

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

问题描述

我无法自动调整包含UICollectionView的UITableViewCell的大小.视图层次结构大致是这样的

UITableViewCell
-- Content View
---- Custom View
------ UIStackView (vertical)
--------- UILabel
--------- UICollectionView (vertical flow layout)

创建或重用UITableViewCell时,collectionView会接收一个数据源,然后开始处理该数据源.我需要将collectionView的所有单元格都显示在tableViewCell中,因此我在这样的自定义视图中观察到collectionView内容大小的变化

    self.collectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)

collectionView完成呈现其内容后,我将读取其contentSize并相应地设置集合视图的高度约束.效果很好.

现在的问题是所有值都可以正确计算,但是tableView中的单元格大小不会改变.控制台中没有自动布局错误或警告,因此我假设所有约束都是正确的. stackView中的标签也可以自动调整大小,并且效果很好.

我可以将有关stackView的新contentSize的信息一直传递到TableViewController,但是我不知道该在哪里调用自动布局以重新计算单个单元格高度的方法.

如果在单元格中collectionView的高度限制与其内容大小不匹配时,我在控制器级别调用tableView.reloadData(),则它确实起作用.但这会导致延迟,并且显然太昂贵了.

如果我将更改为tableView的单元格传递给它,并要求它仅重新加载该单元格,则tableView找不到该单元格的indexPath(可能是因为它仍然不在屏幕上).因此这种方法行不通.

谢谢!

解决方案

首先感谢大家的回应!

拔掉头发两天后,它看起来像 UITableViewCell直到单元被重用,布局才会更新为我解决了该问题.

问题是失踪

    cell.layoutIfNeeded()

末尾的

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

在这里您可以看到现在的样子.设置与原始问题类似.

再次感谢大家的指点!

I am having troubles with an autosizing UITableViewCell which contains a UICollectionView. The view hierarchy is roughly like this

UITableViewCell
-- Content View
---- Custom View
------ UIStackView (vertical)
--------- UILabel
--------- UICollectionView (vertical flow layout)

When the UITableViewCell is created or reused, the collectionView receives a datasource which it then starts to process. I need all cells of the collectionView to show in the tableViewCell, so I observe changes to the collectionView's content size in the Custom View like this

    self.collectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)

Once the collectionView finished rendering its content, I read its contentSize and set the height constraint of the collection view accordingly. This works fine.

The problem now is that all values calculate correctly, but the cell size does not change in the tableView. There are no autolayout errors or warnings in the console, so I assume all constraints are correct. The label in the stackView is also autosizing and this works fine.

I can pass info about the new contentSize of the stackView all the way up to the TableViewController, but I don't know what to call where to force autolayout to recalculate the height of an individual cell.

If I call tableView.reloadData() at the controller level if the height constraint of the collectionView in a cell does not match its content size, it does work. But it causes a delay and is obviously too expensive.

If I pass the cell that changed to the tableView and ask it to reload just the cell, the tableView does not find the indexPath of the cell (probably because it is still off screen). So this approach does not work.

Thanks!

解决方案

First of all thanks to everybody for their responses!

After two days of pulling out my hair, it looks like UITableViewCell layout not updating until cell is reused solved it for me.

The problem was the missing

    cell.layoutIfNeeded()

at the end of

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

Here you can see what it looks like now. The setup is like in the original question.

Again thanks everybody for your pointers!

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

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