UITableViewCell Size 在运行时没有扩展。?

UITableViewCell Size is not expanding at runtime .?

here is a storyboard image. i have take a dynamic tableviewcell 我试过拖放 UITableViewCell 并从情节提要中更改行高,它无法在运行时扩展单元格高度。

Xcode 9.0.1Swift 4中,我遇到了单元格高度在runtime.so时没有增加的问题,请帮助我解决这个问题。

试试这个:

1. 实施 UITableViewDelegate 方法:

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

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

2. 将适当的 constraints 应用到您的 UITableViewCell 以便它可以正确计算自己的 height。正确的 UITableViewCell UI 必须出现。 autolayout 中的任何问题都可能导致您的单元 UI 出现意外行为。

要为行高和估计行高设置自动尺寸,请确保按照以下步骤制作,自动尺寸对 cell/row 高度布局有效。

  • 分配并实现数据源和委托
  • UITableViewAutomaticDimension 分配给 rowHeight & estimatedRowHeight
  • 实施delegate/dataSource方法(即heightForRowAt和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
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension
}



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

对于 UITableviewCell 中的标签实例

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