83

嗨,有很多问题可以回答 of 的动态UITableViewCell高度UITableView。但是,当我这样做时,我觉得很奇怪。

这是一些答案:

这里这里

通常这会回答单元格的动态高度

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension

但就我而言,我想知道这条线不会做任何事情。

UITableView单击拆分视图中的标签栏后,正在查看我的内容。这有帮助吗?

也许我错过了一些东西。谁能帮助我我花了2个小时做傻事。

这些是我对标题的限制,标题可能很长,但标签不是。

在此处输入图像描述

这是我的牢房

在此处输入图像描述

4

19 回答 19

171

为了使 UITableViewAutomaticDimension 工作,您必须设置所有相对于单元格容器视图的左侧、右侧、底部和顶部约束。在您的情况下,您需要将缺少的底部空间添加到标题下标签的超级视图约束

于 2015-05-18T10:32:21.250 回答
30

我以编程方式添加了约束,并且不小心将它们直接添加到单元格中,即不在contentView属性上。添加约束来contentView为我解决它!

于 2016-02-08T09:42:34.307 回答
26

要设置行高和估计行高的自动尺寸,请确保执行以下步骤,自动尺寸对单元格/行高布局有效。

  • 分配和实现tableview dataSource和delegate
  • 分配UITableViewAutomaticDimension给 rowHeight 和estimatedRowHeight
  • 实现委托/数据源方法(即heightForRowAt并返回一个值UITableViewAutomaticDimension给它)

-

目标 C:

// in ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

  @property IBOutlet UITableView * table;

@end

// in ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table.dataSource = self;
    self.table.delegate = self;

    self.table.rowHeight = UITableViewAutomaticDimension;
    self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}

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

    return UITableViewAutomaticDimension;
}

迅速:

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    // Swift 4.2 onwards
    table.rowHeight = UITableView.automaticDimension
    table.estimatedRowHeight = UITableView.automaticDimension


    // Swift 4.1 and below
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension

}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // Swift 4.2 onwards
    return UITableView.automaticDimension

    // Swift 4.1 and below
    return UITableViewAutomaticDimension
}

对于 UITableviewCell 中的标签实例

  • 设置行数 = 0(& 换行模式 = 截断尾部)
  • 设置关于其超级视图/单元格容器的所有约束(顶部、底部、左右)。
  • 可选:设置标签的最小高度,如果你想要标签覆盖的最小垂直区域,即使没有数据。

在此处输入图像描述

注意:如果您有多个具有动态长度的标签(UIElements),应根据其内容大小进行调整:为您希望以更高优先级扩展/压缩的标签调整“内容拥抱和压缩阻力优先级”。

于 2017-10-17T12:01:26.163 回答
19

还要确保单元格中 UILabel 的 Lines 设置为 0。如果设置为 1,它将不会垂直增长。

于 2015-10-03T13:46:33.133 回答
7

我有一个具体案例,我阅读的 99% 的解决方案都不起作用。我想我会分享我的经验。希望它可以提供帮助,因为在解决此问题之前我已经挣扎了几个小时。

我的场景

  • 在一个 ViewController 中使用两个 TableView
  • 我在这些表格视图中加载自定义单元格(xib)
  • 单元格中的动态内容由两个标签组成
  • 页面使用ScrollView

我需要实现的目标

  • 动态 TableView 高度
  • 动态 TableViewCells 高度

您需要检查以使其顺利运行

  • 就像每个教程和答案都会告诉您的那样,为您的标签/内容(顶部、底部、前导和尾随)设置所有约束,并将其设置为 ContentView(Superview)。这个视频教程会很有帮助

  • viewWillAppear我的 ViewController 的方法中,我给我的一个 tableViews ( tableView2) 一个估计的行高,然后使用它UITableViewAutomaticDimension(在所有教程/答案中都可以看到):

        override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        tableView2.estimatedRowHeight = 80
        tableView2.rowHeight = UITableViewAutomaticDimension
    
    }
    

对我来说,之前的这些步骤还不够。我的 TableView 将简单地采用estimatedRowHeight 并将其乘以单元格的数量。嗯。

  • 当我使用两个不同的 tableViews 时,我为它们中的每一个创建了一个插座,tableView1并且tableView2. 允许我在方法中调整它们的大小viewDidLayoutSubviews。随时在评论中询问我是如何做到的。

  • 通过将其添加到viewDidAppear方法中,一个额外的步骤为我修复了它:

    override func viewDidAppear(_ animated: Bool) {
    tableView1.reloadData() // reloading data for the first tableView serves another purpose, not exactly related to this question.
    tableView2.setNeedsLayout()
    tableView2.layoutIfNeeded()
    tableView2.reloadData()
    }
    

希望对某人有所帮助!

于 2017-05-07T09:14:19.437 回答
7

从 iOS 11 和 Xcode 9.3 开始,上述大部分信息都是错误的。 Apple 指南建议设置

    tableView.estimatedRowHeight = 85.0
    tableView.rowHeight = UITableViewAutomaticDimension

WWDC 2017 session 245(使用动态类型构建应用程序,视频大约 16:33)提出了类似的建议。这一切对我来说都没有什么不同。

对于字幕样式的单元格,要获得自定大小的表格视图单元格,只需将标题和字幕的行数(在属性检查器的标签部分中)设置为 0。如果这些标签之一太长并且您没有将行数设置为 0,则表格视图单元格将不会调整其大小。

于 2018-04-23T18:03:17.810 回答
5

忘记删除tableView(heightForRowAt:indexPath:)

像我一样,您可能会不小心忘记删除heightForRowAt方法的样板代码。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 60.0
}
于 2017-02-06T13:36:48.030 回答
5

对于任何可能与我面临同样问题的人:我努力了一个小时才能让它发挥作用。一个简单的 UIlabel,带有上、下左和右约束。我使用 willDisplayCell 将数据传递给单元格,这导致了单元格高度的问题。只要我将该代码放在 cellForRow 中,一切都会正常工作。不明白为什么会这样,可能是在调用 willDisplayCell 之前计算了单元格高度,因此没有内容可以进行正确的计算。我只是想提一下,可能会帮助有同样问题的人。

于 2019-06-17T10:41:21.050 回答
4

它在 iOS 11 及更高版本中对我来说工作得很好,而在 iOS 10 中,解决方案是添加

table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = 200 // a number and not UITableView.automaticDimension

还要确保添加“heightForRowAt”,即使它适用于 iOS 11 及更高版本,如果您支持 iOS 10,则需要它

   func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
于 2019-08-07T13:01:52.620 回答
3

在我的情况下,我有两个 UITableView,我必须将此方法修改为

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 0.0;
    if (tableView == self.tableviewComments) {
        return UITableViewAutomaticDimension;
    }else if (tableView == self.tableViewFriends) {
        height = 44.0;
    }
    return height;
}
于 2017-01-03T17:35:58.223 回答
3

试试这个,简单的解决方案对我有用,

在 viewDidLoad 中写下这段代码,

-(void)viewDidLoad 
{
[super viewDidLoad];
 self.tableView.estimatedRowHeight = 100.0; // for example. Set your average height
 self.tableView.rowHeight = UITableViewAutomaticDimension;
}

在 cellForRowAtIndexPath 中写下这段代码,

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView 
  dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
  }
    cell.textLabel.numberOfLines = 0; // Set label number of line to 0
    cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"menu"];
    [cell.textLabel sizeToFit]; //set size to fit 
    return cell;
 }
于 2018-01-05T14:27:53.860 回答
1

对于我的设置,我首先仔细检查了故事板中的约束UITableViewCell从上到下是明确的,然后不得不在 2 个地方修改代码。

  1. UITableViewDelegatetableView(_:heightForRowAt:)

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
        // return UITableViewAutomaticDimension for older than Swift 4.2
    }
    
  2. UITableViewDelegatetableView(_:estimatedHeightForRowAt:)

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
        // return UITableViewAutomaticDimension for older than Swift 4.2
    }
    

第 1 步和第 2 步,让您仅对特定行应用自动调整大小。要应用于整体UITableView,请使用:

tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = UITableView.automaticDimension
// return UITableViewAutomaticDimension for older than Swift 4.2
于 2018-12-04T10:38:12.517 回答
0

为了使 UITableViewAutomaticDimension 工作,您需要确保将所有 4 个角的约束添加到超级视图。在大多数情况下,它与 UILabel 配合得很好,但有时我发现 UITextView 在 UILabel 当时不能正常工作时会起到作用。所以尝试切换到 UITextView 并确保Scrolling Enabled在情节提要中未选中,或者您也可以以编程方式设置它。

于 2017-10-17T11:53:53.090 回答
0

我有三个水平项目,最左边的有一个向左的约束,中间的一个有顶部、底部、左边的项目和右边的项目。右边有一个右边,左边是中间项。解决方案是向中间项目(我的标签)添加额外的左右约束,这些约束 >= 单元格的某个数字。

于 2017-12-24T00:11:21.280 回答
0

确保单元格中的所有视图都被限制到内容视图而不是单元格。

正如苹果所说:

在单元格的内容视图中布置表格视图单元格的内容。要定义单元格的高度,您需要一个完整的约束链和视图(具有定义的高度)来填充内容视图的顶部边缘和底部边缘之间的区域。

于 2018-01-13T07:12:48.877 回答
0

在此处输入图像描述就我而言,我做了一切,但它并没有为我解决问题,最后,我检查了固定为 3 的标签行。将行值更改为 0 为我解决了问题。所以告诉你它必须是 0 并且必须附加约束 Top、Bottom、Left、Right 才能正常工作。

于 2020-12-03T17:10:35.150 回答
0

一种不常见的解决方案,但可以节省其他人的时间:cellForRowAtIndexPath确保在设置标签文本时没有延迟。由于某种未知的原因,我将它包裹在一个DispatchQueue.main.async块中,这意味着在实际设置标签文本之前返回单元格,因此单元格和标签永远不会扩展到文本的大小。

于 2021-12-15T12:42:42.347 回答
0

对于最新版本的 ios 和 swift apple 已将之前调用的 UITableView.automaticDimension 更改为 UITableView().estimatedRowHeight,因此要设置行高自动使用它像这样: tableView.estimatedRowHeight = 150 //随机估计手动帮助编译器之前设置估计值。tableView.rowHeight = UITableView().estimatedRowHeight

于 2022-01-16T02:02:35.663 回答
-1

我有一个新的解决方案..这对我有用。

基本上UITableViewAutomaticDimension改名为UITableView.automaticDimension...我希望..它不会

于 2019-05-15T02:21:12.227 回答