如何根据内容设置单元格的动态高度

问题描述:

我有一个应用程序,需要在表格单元格中显示用户注释。如何根据Instagram中的单元格内容设置单元格的动态高度?

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)?

检查 UITableViewDelegate文档

- (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.