如何将 UITableView 的宽度设置为等于最大单元格数?里面的宽度?
我有一个表视图,并使用一个自定义单元格,其中有 3 个 UI 元素作为子视图。我已经制作了 UIelements,它们是根据内容大小缩小的标签。现在我的问题是设置单元格根据其 UIElements 缩小并相对调整表格视图宽度。
I have a table view and I using a custom cell which has 3 UI elements as subviews. I have made my UIelements which are labels to shrink as per there content size. Now my problem is to set cell to shrink as per its UIElements and relatively adjust tableview width.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法可能是:
ThreeElementView
,它将添加到单元格的内容视图中systemLayoutSizeFitting
来获取最大值widthNSLayoutConstraint
),widthContraint
可以这样设置:您还可以调用最后 3 个使用
tableView.reloadData()
更新表视图之前的行。假设
data
包含实际的表格数据,宽度计算可能如下所示:因此,对于每一行,您将计算内容的宽度并最终返回最大值。
独立测试
这是上述内容的独立测试。 UI 是通过代码以编程方式构建的,因此结果更容易理解。如果按下按钮,您可以看到表格视图的宽度也通过设置约束来动态调整。
ThreeElementView.swift
ThreeElementCell.swift
ViewController.swift
演示
One way could be to:
ThreeElementView
which will be added to the content view of the cellsystemLayoutSizeFitting
to get the maximum widthNSLayoutConstraint
) to the table viewThe
widthContraint
can be setup like this:You would also call the last 3 lines before updating the table view with
tableView.reloadData()
.Assuming that
data
contains the actual table data, the width calculation could look like this:So for each line you would calculate the width of the contents and finally return the maximum value.
Self-Contained Test
Here is a self-contained test of the above. The UI has been built programmatically in code so that the result is easier to follow. If you press the button, you can see that the width of the tableview then also dynamically adjusts just by setting the constraint.
ThreeElementView.swift
ThreeElementCell.swift
ViewController.swift
Demo