更改方向时更改UITableViewCell高度 [英] Change UITableViewCell Height on Orientation Change
问题描述
我有一个UITableView,其中的单元格包含高度可变的UILabel.我可以使用sizeWithFont:constrainedToSize:lineBreakMode:
来计算标签需要的最小高度,当第一次加载表格视图时,该高度可以正常工作.当我旋转表格视图时,单元格会变宽(这意味着显示内容所需的行更少).有什么办法可以在方向更改动画期间或紧接之前或之后由UITableView重新确定单元格的高度吗?谢谢.
I have a UITableView with cells containing variable-height UILabels. I am able to calculate the minimum height the label needs to be using sizeWithFont:constrainedToSize:lineBreakMode:
, which works fine when the table view is first loaded. When I rotate the table view the cells become wider (meaning there are fewer lines required to display the content). Is there any way I can have the height of the cells redetermined by the UITableView during the orientation change animation or immediately before or after? Thank you.
推荐答案
您的UITableViewController可以实现tableView:heightForRowAtIndexPath:
方法来指定特定行的行高,并且可以查看当前方向([[UIDevice currentDevice] orientation]
)决定返回什么高度.
Your UITableViewController can implement the tableView:heightForRowAtIndexPath:
method to specify the row height for a particular row, and it can look at the current orientation ([[UIDevice currentDevice] orientation]
) to decide what height to return.
要确保在方向更改时再次调用tableView:heightForRowAtIndexPath:
,您可能需要
按照这个问题,然后致电[tableView reloadData]
.
To make sure tableView:heightForRowAtIndexPath:
gets called again when the orientation changes, you'll probably need to
detect the orientation change as described in this question, and call [tableView reloadData]
.
这篇关于更改方向时更改UITableViewCell高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!