如何从NIB读取UITableViewCell(子类)的高度? [英] How do I read a UITableViewCell (subclass)'s height from a NIB?

查看:69
本文介绍了如何从NIB读取UITableViewCell(子类)的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NIB中定义了一个UITableViewCell(实际上是一个子类)。在NIB中,我将帧高设置为183,我还将行高设置为183并勾选自定义。

I have a UITableViewCell (really a subclass) defined in a NIB. In the NIB I've set the frame height to 183, I also set the "Row Height" to 183 and ticked off custom.

在我原来的问题中,所有内容都去了错在这里。看起来我的背高度错了,细胞装在错误的高度,然后调整到合适的高度,造成一个令人讨厌的视觉故障。

In my original question stuff all went wrong here. It looked like I was getting a wrong height back, and the cell loaded at the wrong height and later resized to the right height making a nasty visual glitch.

实际上我正在做一些愚蠢的事情(而且总是回到错误的高度),而且我已经太晚了才能弄明白,所以我向SO寻求帮助然后去睡觉。睡眠(+咖啡)使一切都清晰。

In reality I was doing some stupid stuff (and always returning the wrong height), and it was too late at night for me to figure it out, so I asked SO for help and went to sleep. The sleep (+coffee) made everything clear.

因此,对于记录,如果从NIB加载自定义UITableViewCell,您可以查看frame.size.height和正确的号码回来了。

So for the record, if you load your custom UITableViewCell from a NIB you can look at frame.size.height and the right number comes back.

如果你从 tableView:heightForRowAtIndexPath:返回错误的号码,你可以得到这个小故障,至少如果错误的单元格是表格中的最后一个(我希望问题会以其他方式表现为其他单元格)

If you return the wrong number from tableView:heightForRowAtIndexPath: you can get this glitch, at least if the errant cell is the last one in the table (I expect the problem will manifest in another way for other cells)

所以我已经解决了自己的问题,并希望留下足够的信息给其他任何人打这个。如果有人认为这应该有正式答案,请放心,我计划在一天或3天内接受最好看的一个。

So I've solved my own problem, and hopefully left enough info for anyone else who hits this. If anyone thinks this ought to have an official answer rest assured I plan on accepting the best looking one in a day or 3.

推荐答案

第一个被调用的UITableViewDelegate方法是heightForRowAtIndexPath而不是UITableViewDataSource方法,cellForRowAtIndexPath。

First UITableViewDelegate method, which gets called is heightForRowAtIndexPath not the UITableViewDataSource method, cellForRowAtIndexPath.

你可以只在cellForRowAtIndexPath中获取你的自定义单元格高度,在那里你分配init这个单元格。因此,它不会考虑自定义单元格高度。

You can get your custom cell height only in cellForRowAtIndexPath, where you alloc-init that cell. So, it won't consider custom cell height over here.

它将采用heightForRowAtIndexPath方法中提到的单元格的高度。因为它首先被调用。

It will take the height for the cell which is mentioned in heightForRowAtIndexPath method only. As it gets called first.

这篇关于如何从NIB读取UITableViewCell(子类)的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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