Xcode 9.0.1中的UITableView Swift 4单元格大小在运行时没有扩展。?

时间:2017-10-31 09:24:35

标签: ios swift uitableview tableview row-height

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

Xcode 9.0.1Swift 4中,我面临的问题是细胞高度在运行时没有增加。所以,请帮我解决这个问题。

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:6)

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

  • 分配并实施dataSource和委托
  • UITableViewAutomaticDimension分配给rowHeight& estimatedRowHeight
  • 实施委托/数据源方法(即heightForRowAt并向其返回值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(& line break mode = truncate tail)
  • 相对于其superview / cell容器设置所有约束(顶部,底部,右侧)。
  • 可选:如果您希望标签覆盖最小垂直区域,即使没有数据,也可以设置标签的最小高度。

enter image description here

答案 1 :(得分:2)

试试这个:

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以意外的方式运行。