ios 如何根据内容设置单元格的动态高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6345204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set dynamic height of cell based on content
提问by Zhen
I am have an app which requires to display user comments within a table cell. How can I set the dynamic height of the cell based on the contents of the cell like in Instagram?
我有一个需要在表格单元格中显示用户评论的应用程序。如何像 Instagram 一样根据单元格的内容设置单元格的动态高度?
Basically, I have texts and comments that have different length in different cells. Can anyone advise me on how I can implement the dynamic height method (like instagram)?
基本上,我的文本和评论在不同的单元格中具有不同的长度。谁能告诉我如何实现动态高度方法(如 instagram)?
回答by simon aplin
Check the UITableViewDelegate documentation.
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
Bear in mind you can't reference the cell from this method, so you want to calculate the height of the contents separately and use it to generate the height.
请记住,您无法通过此方法引用单元格,因此您需要单独计算内容的高度并使用它来生成高度。
回答by Simon Lee
回答by Vincent Guerci
You should give a look at three20 library source code, which offers complex cells like you want to do. It might give you answers on how to do this, even if you do not intend to use it:
您应该查看three20 库源代码,它提供了您想要的复杂单元格。它可能会为您提供有关如何执行此操作的答案,即使您不打算使用它:
回答by Legolas
You should refer to this question. The answer is here.
你应该参考这个问题。答案在这里。
Dynamic UITableView height in UIPopoverController (contentSizeForViewInPopover)?
UIPopoverController (contentSizeForViewInPopover) 中的动态 UITableView 高度?