ios 表格视图单元格行高不起作用

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

Table View Cell Row Height doesnt work

iosswiftuitableviewrow-height

提问by Agung Laksana

enter image description here

在此处输入图片说明

I have tried to hard code the row height in the table view cell. After running the program it looks like only one line. I suspect it is because of the height of the row of the table view.

我试图对表格视图单元格中的行高进行硬编码。运行程序后,它看起来只有一行。我怀疑这是因为表格视图的行高。

Could you please tell me what went wrong in here?

你能告诉我这里出了什么问题吗?

回答by Irfan

In Xcode 9, tableviewautomatically create estimated cells based on Autolayout. Since you have not provide autolayout, that's why you are getting this.

Xcode 9 中tableview自动创建基于Autolayout. 由于您没有提供自动布局,这就是您得到这个的原因。

You need to disable estimated and automatic cell size by selecting tableview and uncheck the two options:

您需要通过选择 tableview 并取消选中两个选项来禁用估计和自动单元格大小:

enter image description here

在此处输入图片说明

Give value you want to Row height, and cell will be created accordingly.

给你想要的行高值,将相应地创建单元格。

Hope this can help.

希望这能有所帮助。

回答by Leo

Add this code in your UITableViewDelegate

在您的 UITableViewDelegate 中添加此代码

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

回答by luckyShubhra

Set row height as well as estimated row height as Automatic in viewDidLoad()as

将行高以及估计的行高设置为 Automatic in viewDidLoad()as

table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension

In heightForRowAt:set height as UITableViewAutomaticDimensionas

heightForRowAt:设置高度UITableViewAutomaticDimension

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Note: Set number of lines of label long restaurant nameas 0.

注意:设置标签的行数long restaurant name为 0。

回答by Krunal

Ensure following steps to make, auto dimension effective for cell/row height layout. (Here is sample code for UILabel and how to set content specific height for table cell.)

确保按照以下步骤使单元格/行高布局的自动尺寸生效。(这是 UILabel 的示例代码以及如何为表格单元格设置内容特定高度。)

  • Assign and implement dataSource and delegate
  • Assign UITableViewAutomaticDimensionto rowHeight & estimatedRowHeight
  • Implement delegate/dataSource methods (i.e. heightForRowAtand return a value UITableViewAutomaticDimensionto it)
  • 分配和实现数据源和委托
  • 分配UITableViewAutomaticDimension给 rowHeight 和estimatedRowHeight
  • 实现委托/数据源方法(即heightForRowAtUITableViewAutomaticDimension为其返回值)

-

——

@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
}

For label instance in UITableviewCell

对于 UITableviewCell 中的标签实例

  • Set number of lines = 0 (& line break mode = truncate tail)
  • Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
  • Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.
  • 设置行数 = 0(& 换行模式 = 截尾)
  • 设置与其父视图/单元格容器相关的所有约束(顶部、底部、左侧)。
  • 可选:设置标签的最小高度,如果你想要标签覆盖的最小垂直区域,即使没有数据。

enter image description here

在此处输入图片说明

回答by findusl

Following this guidewhich worked for me, you have to add a bunch of constraints and then the rowheight is calculated automatically.

按照对我有用的本指南,您必须添加一堆约束,然后自动计算行高。

In short, what I did: (adding and modifying constraints are the two rightmost buttons bottom right of the storyboard view)I at first selected all views in the prototype cell, deleted all constraints (there is a nice option on the rightmost button), then added all missing constraints (another nice option there) and last I chose the second button from the right and clicked all the red lines. You can do this while having all views selected at the same time. And then it worked.

简而言之,我所做的:(添加和修改约束是故事板视图右下角最右边的两个按钮)我首先选择了原型单元格中的所有视图,删除了所有约束(最右边的按钮上有一个不错的选项),然后添加了所有缺少的约束(那里是另一个不错的选择),最后我选择了右侧的第二个按钮并单击了所有红线。您可以在同时选择所有视图的同时执行此操作。然后它起作用了。

回答by gbarnett

You can manually do it in your tableviewdelegate:

您可以在 tableviewdelegate 中手动执行此操作:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

回答by Abhishek Binniwale

  1. Set number of lines to 0
  2. Set
  1. 将行数设置为 0
   yourTableView.rowHeight = UITableView.automaticDimension and 
   yourTableView.estimatedRowHeight =  UITableView.automaticDimension
  1. return UITableView.automaticDimensionin tableView(_:heightForRowAt:)delegate method

  2. Set all constraints . Bottom constraint is important to increase table view cell height .

  1. UITableView.automaticDimensiontableView(_:heightForRowAt:)委托方法中返回

  2. 设置所有约束。底部约束对于增加表格视图单元格高度很重要。

Your all set to go

你准备好了

回答by Mansi

If you want the cells to occupy space according to its content, you need to return UITableViewAutomaticDimensionin the table view delegate methods heightForRowAt: and estimatedHeightForRowAt:

如果您希望单元格根据其内容占用空间,则需要在表视图委托方法heightForRowAt: 和estimatedHeightForRowAt 中返回UITableViewAutomaticDimension