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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:21:49  来源:igfitidea点击:

How to set dynamic height of cell based on content

iosobjective-cuitableview

提问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 一样根据单元格的内容设置单元格的动态高度?

enter image description here

在此处输入图片说明

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.

检查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.

请记住,您无法通过此方法引用单元格,因此您需要单独计算内容的高度并使用它来生成高度。

回答by Simon Lee

Combine my dynamic label category herewith my dynamic table cell height here. Perfect combination!

结合自己的动态标签类别这里有我的动态表格单元格高度这里。完美搭配!

回答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 库源代码,它提供了您想要的复杂单元格。它可能会为您提供有关如何执行此操作的答案,即使您不打算使用它:

three20 Cells

三 20 细胞

回答by Legolas