swift - UITableView : automatically set height for the row and fix it with max height constant

标签 swift height row tableview cell

我需要自动设置行的高度。我用

theTable.estimatedRowHeight = 60
theTable.rowHeight = UITableViewAutomaticDimension

但是高度不固定。我需要固定高度,例如 MAX 60 高度,默认高度为 40。使用 estimatedRowHeight 高度不固定,当字符串很长时,单元格会占据所有屏幕。如何解决?

最佳答案

您需要将表格 View 单元格的所有内容放在一个 View 中(我们称之为 MainView),并将 MainView 顶部、前导和尾部边缘约束设置为容器 View 。还设置一个小于或等于 60 的高度限制,并具有所需的优先级。之后将您的标签放在 MainView 中设置顶部,前导和尾随约束 MainView 和底部空间 MainView 约束具有高优先级。同样在您的标签上将行数设置为 0,并将首选宽度设置为显式。这样,如果标签内容高度小于 60,MainView 将缩小并且仍然匹配小于或等于 60 的高度约束。否则,如果内容高度大于 60,则 MainView 约束的底部空间将中断而不会引起问题,因为它优先级小于 MainView 高度限制。

最后重写tableview委托(delegate)方法如下

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
   return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 40
} 

关于swift - UITableView : automatically set height for the row and fix it with max height constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30349202/

相关文章:

ios - Swift 中的异步 NSOperation 依赖

ios - 将 segue 从 UITableViewCell 推送到 Swift 中的 ViewController

Android ListView 行填充/边距

带有字体大小的 Css 自动高度

excel - 根据某列中的目标词列表提取行

swift - 删除 Gamescene.sks 文件会将设备上的 CPU 提高到 25%

ios - 无法将类型 'UIView' 的值分配给类型 --class 符合 UIView--

asp.net - 如何从 C# 代码隐藏设置 iframe 的最小高度?

c# - 在 WPF C# 中删除命名行

表格的 html5 和 css3 高度就像其中最高的一样