Q: 如何改变 UITableView 中的单元格大小,以显示所有标签?

Q: How can I change the cell size in UITableView, to show all the labels?

我几乎完成了 tableview 单元格的制作,数据模型中的所有内容都显示在 UI 中,所以我认为连接正常。问题是我的原型单元只显示了它应该显示的部分内容。我在单元格中有 3 个标签,但它只显示一个半。 the text is stuck to the left(I wanted to put it in center), and the textlabel is cut off so it doesn't show in the UI。 它应该看起来像 this.

我试过了

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

ViewController 中的此代码使我的单元格自动设置标签的高度,但它不起作用。 我还使用了垂直堆栈视图并进行了约束,因此它完全填充了单元格,而且它似乎没有做出任何改变。

请帮忙!

您必须使用实现 UITableViewDelegate 协议和函数

func tableView(_ tableView: UITableView, 
     heightForRowAt indexPath: IndexPath) -> CGFloat

您可以在哪里 return 每个单元格的自定义高度

您可以在 Storyboard 中执行此操作 - 您只需将 UITableView 中的单元格拖动到更大即可。 UI 调整 table 视图单元格的大小与调整故事板中任何 UI 元素的大小相同。不过,其余的人也会如此。希望这对您有所帮助!