简体   繁体   English

iOS计算单元格的高度

[英]iOS Calculate Height For Cell

I have a problem with Apple's dynamic resizing cells such that the logic for the layout of my cells isn't as cut and dry as just a few stacked growing UILabels. 我对Apple的动态调整单元格大小有疑问,以至于我的单元格布局逻辑不像几个堆叠的UILabel那样枯燥乏味。

As a result, I can't really use the dynamic resizing option they've provided and so I need to manually calculate the height of my cell using NSString boundingRect methods. 结果,我不能真正使用它们提供的动态调整大小选项,因此我需要使用NSString boundingRect方法手动计算单元格的高度。

That's fine - it works, but I end up needing to store a lot of constants that keep track of my auto layout constraints. 很好-可以,但是我最终需要存储很多常量来跟踪我的自动布局约束。 I feel like this is counter intuitive to what auto layout is supposed to do for me, so I'm not sure if this is the correct way to implement heightForRowAtIndexPath. 我觉得这与自动布局应该为我做什么相反,因此我不确定这是否是实现heightForRowAtIndexPath的正确方法。

I essentially have to go and copy my constraints into a constant and then use those values in a class method to generate my heights. 实际上,我必须将约束复制到常量中,然后在类方法中使用这些值来生成我的身高。 Apple provides very little internal insight into how UITableViewAutomaticDimension works, but it's clear that the height of the cell is still calculated BEFORE it is laid out. Apple对UITableViewAutomaticDimension的工作原理几乎没有内部了解,但是很显然,在布局之前,仍要计算单元的高度。 Thus I can't really add any complex logic to it unless I know what methods are called before. 因此,除非我之前知道什么方法,否则我无法真正添加任何复杂的逻辑。

Any ideas on what I should do, or if my approach is ok? 关于应该怎么做的任何想法,或者我的方法还可以吗?

The common solution I can offer is to add a height constraint fro your cell and change the constraint according to your needs, whatever they are. 我可以提供的常见解决方案是在单元格中添加高度限制,并根据您的需要更改限制,无论它们是什么。 UITableViewAutomaticDimension will resize cell to the height you specify with this constraint automatically. UITableViewAutomaticDimension会自动将单元格的大小调整为使用此约束指定的高度。

If your table cell is custom then its easier if you use a method like configureCell and pass the indexPath to it from cellForRowAtIndexPath and then determine the layout based on the data that you have and see where you want the left and right label to be placed. 如果您的表格单元格是自定义的,那么使用诸如configureCell类的方法并从cellForRowAtIndexPath将indexPath传递给它,然后根据您所拥有的数据确定布局并查看要放置左右标签的位置会更容易。 Once you have done this store the height required in the model or perhaps another array that has the same number of rows as your table and use it to return in heightForRowAtIndexPath . 完成此操作后,将所需的高度存储在模型中,或者存储在与表中行数相同的另一个数组中,并使用它返回heightForRowAtIndexPath

This is easier and gives you flexibility without having to fiddle with too many delegate methods of table view. 这更容易,并且为您提供了灵活性,而无需摆弄过多的表视图委托方法。 Centralise your layout logic in one place. 将布局逻辑集中在一个地方。 Another alternative is to override layoutSubviews of the table view cell and calculate the height there and store it. 另一种选择是重写表格视图单元格的layoutSubviews并在其中计算高度并将其存储。

If you want your tableview cell to be assigned a default height, you can use estimatedHeightForRowAtIndexPath: available in UITableViewDelegate. 如果希望为表视图单元格分配默认高度,则可以使用UITableViewDelegate中提供的EstimatedHeightForRowAtIndexPath:。 As per Apple guidelines: 根据Apple准则:

// Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table. //使用estimateHeight方法快速计算猜测值,这将允许快速加载表。 // If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there. //如果实现了这些方法,则上面的-tableView:heightForXXX调用将被推迟,直到准备好显示视图为止,因此可以在其中放置更昂贵的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM