ios Swift 动态表格单元格高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36574052/
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-31 08:54:09  来源:igfitidea点击:

Swift dynamic table cell height

iosuitableviewheightios-autolayout

提问by SpyZip

I have tableview with prototype cell, in the cell I have imageview and some text under. The text label is one laine in the prototype cell, but sometimes it is more than one line, I'm loading the data to the table view after server call. The label in storyboard Lines are set to 0 and Line Break to Word Wrap. I also tried http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

我有带有原型单元格的表格视图,在单元格中我有图像视图和一些文本。文本标签是原型单元格中的一行,但有时不止一行,我在服务器调用后将数据加载到表视图。情节提要 Lines 中的标签设置为 0,Line Break 设置为 Word Wrap。我也试过http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

but no effect. If I use Using Auto Layout in UITableView for dynamic cell layouts & variable row heightseverything works because the label text is predefined, but I'm loading the data from server and I reload the tableView after the API call, and then the label is just one line.

但没有效果。如果我在 UITableView 中使用Using Auto Layout for dynamic cell layouts & variable row heights一切正常,因为标签文本是预定义的,但我从服务器加载数据并在 API 调用后重新加载 tableView,然后标签只是一条线。

回答by iVarun

You should use UITableViewAutomaticDimensionprovides a solution for displaying dynamic content.

您应该使用UITableViewAutomaticDimension提供了一种显示动态内容的解决方案。

Use below code in viewDidLoad:

在以下代码中使用viewDidLoad

tableView.estimatedRowHeight = YourTableViewCellHeight
tableView.rowHeight = UITableView.automaticDimension

Read more from here

从这里阅读更多

Hope it helps. :)

希望能帮助到你。:)

回答by Ankit Kushwah

make sure UILabel height constraint not to be constant, if constant it have to set Relation greater than or equal tofrom attribute inspector

确保 UILabel 高度约束不是恒定的,如果恒定它必须设置 Relation大于或等于来自属性检查器

make sure UITableViewestimate row height set as per your.

确保 UITableViewestimate 行高根据您的设置设置。

self.TableName.estimatedRowHeight = 85

and write in UITableViewCellclass

并在UITableViewCell课堂上写

 Cell.lbl_name.sizeToFit()


 Cell.lbl_name.numberOfLines = 0

Make sure your UITableViewDelegatemethod

确定你的UITableViewDelegate方法

func tableView(tableView: UITableView,
    heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{

 return UITableViewAutomaticDimension
}