ios 基于 UILabel 文本量的 UITableViewCell 自动高度

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

UITableViewCell auto height based on amount of UILabel text

iosobjective-cuitableviewstoryboardxcode8

提问by HurkNburkS

I have a bit of a tricky set up in my storyboard, I have a UIViewController that holds a UITableViewController. Within the UITableViewController I have several prototypecells I have linked to subclassed uitableviewcell objects in code.

我在我的故事板中设置了一些棘手的设置,我有一个 UIViewController 包含一个 UITableViewController。在 UITableViewController 中,我有几个原型单元,我已经链接到代码中的子类 uitableviewcell 对象。

Using constraints and storyboard I would like to change the height of my prototype cell depending on the size the UILabel ends up being which is dependant on the text going into it.

使用约束和故事板,我想根据 UILabel 最终的大小来更改原型单元格的高度,这取决于进入其中的文本。

Currently I have a

目前我有一个

UIViewController
-- UITableViewController
-- -- UITableViewCell ((melchat) prototype cell)
-- -- -- ContentView
-- -- -- -- UIView ((background) view with drop shadow card type effect)
-- -- -- -- -- UIImageView (Avatar)
-- -- -- -- -- IUlabel (dynamic (depending on code/input) multi line UILabel)

Some how I would like the UILabel to resize the UIView (background) then in turn effect the height of that UITableViewCell.

我希望 UILabel 调整 UIView(背景)的大小,然后反过来影响 UITableViewCell 的高度。

I am using XCode 8.2.1

我正在使用 XCode 8.2.1

I have taken a screen shot of the layout in storyboard and constraints applied.

我在情节提要中截取了布局并应用了约束。

enter image description here

在此处输入图片说明

Update

更新

I have updated my constraints to pretty much all go back to ContentView and have updated uilabel line count to 0 and then also implemented the UITableViewAutomaticDimension code but its still not working. Please see code and screen shots below.

我已将我的约束更新为几乎所有返回到 ContentView 并将 uilabel 行数更新为 0,然后还实现了 UITableViewAutomaticDimension 代码,但它仍然无法正常工作。请参阅下面的代码和屏幕截图。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;

}

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Scriptable

To go a little further on Dao's answer..

在道的回答上更进一步..

He is correct, you need to use UITableViewAutomaticDimension

他是对的,你需要使用 UITableViewAutomaticDimension

Also, you need to ensure that you setup your constraints in a way that all of the content in the cell is constrained to the cells contentView. So your label will likely need constraints such as

此外,您需要确保以将单元格中的所有内容都约束到单元格 contentView 的方式设置约束。因此,您的标签可能需要约束,例如

  • Leading constraint to ImageView
  • Top constraint to contentView
  • Bottom constraint to contentView
  • Trailing constraint to contentView
  • ImageView 的引导约束
  • contentView 的顶部约束
  • 对 contentView 的底部约束
  • contentView 的尾随约束

Make sure that you set the UILabel to multiline (or lines = 0) and it should work.

确保将 UILabel 设置为多行(或行 = 0)并且它应该可以工作。

If you are using the heightForRowAt delegate functions ensure you return UITableViewAutomaticDimension

如果您使用的是 heightForRowAt 委托函数,请确保您返回 UITableViewAutomaticDimension

回答by ?ào Minh H?t

Use UITableViewAutomaticDimension

UITableViewAutomaticDimension

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300

回答by Brijesh Shiroya

step1:Give below constraint to UIView that is inside to the contentView.

步骤 1:给 contentView 内部的 UIView 提供以下约束。

  • Leading constraint to contentView
  • Top constraint to contentView
  • Bottom constraint to contentView
  • Trailing constraint to contentView
  • 对 contentView 的引导约束
  • contentView 的顶部约束
  • 对 contentView 的底部约束
  • contentView 的尾随约束

step2: Give below constraint to UIlabel

步骤 2:给 UIlabel 提供以下约束

  • Leading constraint to UIImageView
  • Top constraint to UIView
  • Bottom constraint to UIView
  • Trailing constraint to UIView
  • UIImageView 的引导约束
  • UIView 的顶部约束
  • 对 UIView 的底部约束
  • UIView 的尾随约束

step3: Then select your Trailing constraint of IUlabel and select edit option and then select constant and select greaterThanEqual option.

步骤3:然后选择IUlabel的尾随约束并选择编辑选项,然后选择常量并选择greaterThanEqual选项。

step4: set label's numberofline = 0

step4:设置标签 numberofline = 0

step5: Add this code into viewDidLoad()

步骤5:将此代码添加到 viewDidLoad()

yourtableview.estimatedRowHeight = 80.0
yourtableview.rowHeight = UITableViewAutomaticDimension

回答by Gurjinder Singh

Swift 4.2 and 5. As mention above you can set UITableView object property or

斯威夫特 4.2 和 5。如上所述,您可以设置 UITableView 对象属性或

tblView.rowHeight = UITableView.automaticDimension 
tblView.estimatedRowHeight = 300

you can also define the same by implementing UITableViewDelegate methods

你也可以通过实现 UITableViewDelegate 方法来定义相同的

extension ViewController: UITableViewDelegate {
      func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 300
      }
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
      }
    }

回答by Ricardo Alves

After populating the UILabel and applying constrains, get the height of the label and set it as the height of the UIView.

填充 UILabel 并应用约束后,获取标签的高度并将其设置为 UIView 的高度。

After that call -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPathand pass the height of the UILabel/UIView so that it gets the actual height for that row

在该调用之后-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath并传递 UILabel/UIView 的高度,以便它获得该行的实际高度