动态桌子高度。行高的冗余计算?
在表格视图中,我使用具有动态高度的自定义 UITableViewCells。布局算法很复杂,因为不仅有动态文本块,而且有不同高度的图像。当调用适当的 heightForRowAtIndexPath 时,teble 视图单元尚未渲染完成。我需要计算两次高度吗?一次用于 heightForRowAtIndexPath,一次用于 cellForRowAtIndexPath?或者有一个模式如何将其结合起来?
In a table view I use custom UITableViewCells with dynamic height. The layout algorithm is complicated since there are not only dynamic text blocks but also images with different height. When the appropriate heightForRowAtIndexPath is called the teble view cell has not been rendered do far. Do i have to calculate the height twice? Once for heightForRowAtIndexPath and onece for cellForRowAtIndexPath? Or is there a Pattern how to combine that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必编写两次计算代码。您可以在
cellForRowAtIndexPath:
中完成所有艰苦的工作,然后使用结果在 height 方法中获取高度。例如,您的 height 方法可能如下所示(使用一些短名称来帮助其适应):这段代码的不好的部分是,在首次显示表之前,将为表中的每一行调用此方法(操作系统不会this),这可能会在您的应用程序中显示为滞后。如果您的高度随着时间(每行)而变化,那么最好有一些更快的方法来计算高度,是的,这需要更多的工作。但是,如果您的行高随着时间的推移始终相同(每行),那么您可以只缓存高度。
参考一些可用于此目的的缓存代码:
http://bynomial.com/blog/?p=90
You don't have to write the computation code twice. You can do all the hard work in
cellForRowAtIndexPath:
and then use the result to get the height in the height method. For example, your height method could look like this (using some short names to help it fit):The bad part of this code is that this method will get called for every row in your table before the table is first displayed (the OS does this), which may appear as lag in your app. If your heights change over time (per row), then it would be good to have some faster way to compute the height, which, yes, is a lot more work. However, if your row heights are always the same (per row) over time, then you can just cache the heights.
Reference with some caching code you can use for this:
http://bynomial.com/blog/?p=90