您好。我已经尝试在Goodle找到一些关于它的信息,但没有什么可以帮助我。
我是初学者。请帮帮我。
我想在TableViewCell
中自动布局TableViewController
。
现在它在IB,预览和模拟器中看起来很好,但只有这个屏幕大小,当然。
然后我添加了5个约束,正如我在一个StackOverflow回答中看到的那样,对于Cell的每个子视图,如下所示:
现在,当我运行项目时看起来崩溃了:
为什么?
答案 0 :(得分:2)
您可以将UIStackView
用于此类嵌套视图。它只是减少了开发人员需要应用的约束数量。
示例:强>
<强> 1。 UITableViewDataSource 强>
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
return tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
}
<强> 2。故事板:查看层次结构和约束:
第3。输出:强>
我没有使用height/estimatedHeight
UITableViewDelegate
方法。它将由单元格(if the constraints are right and the cell is able to calculate its own height)
自动计算。
答案 1 :(得分:-1)
如果您将这5个约束添加到单元格的每个子视图中,那么它就不正确。此外,contentView也是单元格的子视图,您可以修改其宽度 - 从而显示宽度减小。另外,每个子视图(不应该包含contentView)应该具有不同的约束。
您还可以让Xcode使用 UITableViewAutomaticDimension 根据您的约束自动估算细胞的高度。