UITableViewCell与'三栏'Autolayout

时间:2015-09-29 11:31:36

标签: uitableview autolayout ios9

我有一个UITableViewCell,其布局如下:

-----------------------------------
[Label1] [Label2         ][Button1]
[Label3] [Label4         ][Button2]
                          [Button3]
------------------------------------

我想将此视为三列,其中[Label2]和[Label4]可以包含可变内容,这意味着这些标签的高度可能会发生变化。 [Label1],[Label3]和[Button1],[Button2]和[Button3]的高度和宽度是固定的。

Column 1 - fixed width
[Label1]
[Label3]

Column 2 - variable width and height
[Label2]
[Label4]

Column 3 - fixed width and height
[Button1]
[Button2]
[Button3]

使用Autolayout实现此布局的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

标签1约束在单元格内容视图的左侧和顶部,具有固定的宽度和高度。

标签3约束在单元格内容视图的左侧,具有固定的宽度和高度。

按钮1,2,3约束在单元格内容视图的右侧,固定宽度和高度。按钮1和2分别垂直居中于标签1和标签3。

标签2的前导约束为标签1,尾部约束为按钮1.它的顶部约束为标签1,底部应约束到标签3的顶部。

标签4的前导约束为标签3,尾部约束为按钮2.它的顶部约束为标签3,底部应限制在按钮3的顶部。

按钮3底部约束到单元格内容视图的底部。

标签2和标签4应设置为0行数。

您可能还希望将标签1 - 标签3和按钮2 - 按钮3之间的间距设置为大于或等于8,如果标签2和标签4中没有文本,则他们希望折叠整个单元格,

您的表格视图应使用自我调整大小的单元格(tableView.rowHeight = UITableViewAutomaticDimension)。