有UITableViewCell的调整本身带有自动布局 [英] Have UITableViewCell resize itself with autolayout

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

问题描述

我有一个的UITableViewCell 3的标签,并标签设定所以他们会换行。如果自动换行文本进入下一个单元格。我如何获得自动版式扩大基于内容的单元格,而无需编写的 heightForRowAtIndex 方法code?是不是有一个约束,我可以用它来自动调整的基础上的内容查看电池?

I have 3 labels in a UITableViewCell and have the labels set so they will wordwrap. If they word wrap the text goes into the next cell. How do I get AutoLayout to expand the cell based on the content without having to write code in the heightForRowAtIndex method? Isn't there a constraint I can use to automatically adjust the cell based on the contentView?


细胞看起来不错,如果文本没有在标签换行。一旦它包装是出现问题时,我想有细胞大小以适合的内容,并有底部标签和底部之间的间距相同,因为是顶部和顶部标签之间。

The cell looks fine if the text doesn't wrap in the label. Once it wraps that is when the problem occurs and I would like to have the cell resize to fit the content and have the same spacing between the bottom label and the bottom as there is between the top and top label.

推荐答案

不幸的是,不,你不能做到这一点。表视图首先计算其自己的总高度,并具有每个小区的大小的固定的想法,因为它们加载,这将无法确定这是从在外侧的高度和它不会让布局约束改变一个单元的高度。

Unfortunately no, you can't do this. A table view calculates its own total height first and has a fixed idea of the size of each cell as they load, it won't determine it's height from the outside in and it won't let layout constraints change the height of a cell.

如果你仔细想想表是如何工作的,与细胞再利用,那么你就无法真正面积从它的细胞表而不加载于每个细胞并将其添加到滚动视图,并进行布局通过对整个事情。这可能会导致性能相当差。

If you think about how tables work, with cell reuse, then you couldn't really size the table from its cells without loading in every cell and adding it to the scrollview, and performing a layout pass on the whole thing. That would probably lead to quite poor performance.

您可以用填充自由细胞实验(即一个单元格你刚刚实例化,而不是添加到表),并铺设出在你的数据源的每一行计算heightForRow时。

You could experiment with populating a "free" cell (i.e a cell you've just instantiated, not added to a table) and laying it out for each row in your datasource when calculating heightForRow.

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

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