estimatedHeightForRowAtIndexPath实际上可以改变最终的“正确”状态。一排的高度? [英] estimatedHeightForRowAtIndexPath can in fact change the final "correct" height of a row?

查看:369
本文介绍了estimatedHeightForRowAtIndexPath实际上可以改变最终的“正确”状态。一排的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用estimatedHeightForRowAtIndexPath时发现了一个惊人的问题或反直觉行为

I've just discovered an astounding problem or counter-intuituve behaviour when using estimatedHeightForRowAtIndexPath

(1)我的表行高度差别很大。最终结果的范围可以从111到大约400.

(1) My table has wildly varying row heights. The final results can range from 111 to about 400.

(2)我绝对完美地计算每一行的高度。我手边有一个数组,也就是说缓存。

(2) I absolutely perfectly calculate each row height. I have these on hand in an array, that is to say cached.

{请注意,这正是Apple工程师现在推荐的......例如,点5 .. 在UITableView中使用自动布局进行动态单元格布局&安培;变量行高度}

{Note that this is exactly what, apparently, Apple engineers now recommend...example, point 5 .. Using Auto Layout in UITableView for dynamic cell layouts & variable row heights}

(3)当heightForRowAtIndexPath要求高度时,我确实给它绝对正确的高度。

(3) When heightForRowAtIndexPath asks for a height, I do give it absolutely the correct height.

(4)当我构建单元格时,实际上,我将它构建到正确的高度(如(2)和(3)中所示)。

(4) WHen I build the cell, indeed, I build it to exactly the correct height (as in (2) and (3)).

{注意 - 当然是iOS最终调整单元格的高度,而不是我。}

{Note - of course it's iOS that finally sizes the height of a cell, not "me".}

这一切都完美无缺。

即,每个单元格都是由iOS构建的,其高度恰好是heightForRowAtIndexPath中给出的高度。

ie, each cell is built by iOS at exactly the height given in heightForRowAtIndexPath.

现在,我添加代码...

Now, I add the code ...

-(CGFloat)tableView:(UITableView *)tableView
    estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

事实上,表格不再工作..... .......行星高度成为随机!!!

是否有人看到过这种令人难以置信的行为?

我做了各种测试,试图确定thinkHeightForRowAtIndexPath的地狱关系。起初,我认为它可能提供高度的下限。所以,150 ..甚至我的小细胞都会错误地达到150高度。但事实并非如此。

I did various tests to try to determine the relationship of what the hell estimatedHeightForRowAtIndexPath does. At first, I thought it might provide a lower bound on the height. So, 150 .. even my smaller cells would incorrectly be 150 height. But that is not the case.

我认为它可能会做这样的事情:说你的estimatedHeightForRowAtIndexPath值是150.有时使用150行(事实上) ()证明是大小或更小,但有时它从heightForRowAtIndexPath获得实际大小。

I think it MIGHT be doing something like this: say your estimatedHeightForRowAtIndexPath value is 150. It is sometimes using 150 for rows that (in fact() prove to be that size or less, but sometimes it goes for the real size from heightForRowAtIndexPath.

另一方面,如果你为estimatedHeightForRowAtIndexPath输入一个值,它比实际存在的要小(比如我的例子中的100)它几乎完全不起作用你得到的东西似乎只是单元格上的随机高度。

On the other hand, if you put in a value for estimatedHeightForRowAtIndexPath that is smaller than will ever actually exist (say 100 in my example) it pretty much "utterly doesn't work" you just get what can only seem to be random heights on the cells.

非常高的单元似乎工作正常,可能类似如果从heightForRowAtIndexPath的高度是估计的两倍,那么它确实使用实际高度

very high cells seem to work correctly, perhaps something like "if the height from heightForRowAtIndexPath is double the estimated, then it does use the real height"

到很明显,它似乎永远不会使细胞太小,但它往往使它们太大。

To be clear, it seems that it never makes a cell too small, but it often makes them too big.

要清楚,我使用autolayout,它是你必须构建的单元格类型。(我是害怕我不知道自动布局如何。)这只是Xcode5 / iOS7 +。

To be clear, I am not using autolayout, it's the type of cell you just have to build. (I'm afraid I have no idea how this goes with autolayout.) This is Xcode5/iOS7+ only.

推荐答案

更新后的答案

经过进一步调查后,结果显示它只需要自动布局不正确。这是我的示例代码,需要自动布局。我在 DynamicHeightCell 中做了一些修改,它现在可以使用或不使用自动布局。

After further investigation, it turns out "it simply requires Auto Layout" is incorrect. It was my sample code that required Auto Layout. I made a slight modification in DynamicHeightCell and it now works with or without Auto Layout.

原始答案

它只需要自动布局。这并不奇怪,但是应该完全记录下来。

It simply requires Auto Layout. Not surprising, really, but should absolutely be documented.

这是一个展示 tableView:estimatedHeightForRowAtIndexPath:工作的工作项目正确使用自动布局。如果在故事板中关闭自动布局,则其行为与您所描述的相同。

Here is a working project demonstrating tableView:estimatedHeightForRowAtIndexPath: working correctly with Auto Layout. If you turn off Auto Layout in the storyboard, it behaves as you've described.

估计行高演示

这篇关于estimatedHeightForRowAtIndexPath实际上可以改变最终的“正确”状态。一排的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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