sim*_*ity 69 xcode row-height uitableview ios swift
可变长度的动态文本被注入tableview单元格标签.为了使tableview单元格的高度动态调整,我实现了viewDidLoad():
self.tableView.estimatedRowHeight = 88.0
self.tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)
这适用于尚未UITableViewAutomaticDimention滚动到的单元格(在滚动到单元格时调用),但不适用于在加载具有数据的表格时最初在屏幕上呈现的单元格.
我试过简单地重新加载数据(如许多其他资源中所建议的):
self.tableView.reloadData()
Run Code Online (Sandbox Code Playgroud)
在这两个viewDidAppear()和viewWillAppear(),这是无济于事.我迷了..有没有人知道如何让xcode渲染最初在屏幕上加载的单元格的动态高度?
如果有更好的替代方法,请告诉我.
iDh*_*val 99
试试这个:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
编辑
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
Swift 4.2
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Run Code Online (Sandbox Code Playgroud)
定义以上两种方法.
它解决了这个问题.
PS:这需要顶部和底部约束才能工作.
ami*_* rt 34
用这个:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
Run Code Online (Sandbox Code Playgroud)
并且不要使用:heightForRowAtIndexPath委托功能
此外,在故事板中不要设置包含大量数据的标签的高度.给它top, bottom, leading, trailing约束.
Vla*_*iak 14
SWIFT 3
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 160
Run Code Online (Sandbox Code Playgroud)
和!!!在storyBoard中:您必须为Label设置TOP&BOTTOM约束.没有其他的.
sim*_*ity 11
这个奇怪的bug是通过Interface Builder参数解决的,因为其他答案没有解决问题.
我所做的就是使默认标签尺寸大于可能的内容,并将其反映在estimatedRowHeight高度上.以前,我在Interface Builder中设置默认行高,88px并在我的控制器中反映出来viewDidLoad():
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 88.0
Run Code Online (Sandbox Code Playgroud)
但那没用.所以我意识到内容不会变得比可能更大100px,所以我将默认单元格高度设置为108px(大于潜在内容)并在控制器中如此反映viewDidLoad():
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 108.0
Run Code Online (Sandbox Code Playgroud)
这实际上允许代码将初始标签缩小到正确的大小.换句话说,它从未扩展到更大的尺寸,但总是可以缩小......而且,不需要额外self.tableView.reloadData()的viewWillAppear().
我知道这不包括高度可变的内容大小,但这适用于我的情况,其中内容具有最大可能的字符数.
不确定这是否是Swift或Interface Builder中的错误,但它就像魅力一样.试试看!
设置行高和估计行高的自动尺寸并确保以下步骤:
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Set automatic dimensions for row height
// Swift 4.2 onwards
table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = UITableView.automaticDimension
// Swift 4.1 and below
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// Swift 4.2 onwards
return UITableView.automaticDimension
// Swift 4.1 and below
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
例如:如果您的UITableviewCell中有标签,那么,
这是具有动态高度约束的样本标签.

对于 Swift 3,您可以使用以下内容:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
UITableView的动态大小调整单元需要2件事
在viewDidLoad()中调用TableView的这些属性
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
Run Code Online (Sandbox Code Playgroud)这是一个关于用swift 3编写的自调整大小(动态表视图单元格)的精彩教程.
小智 5
除了别人说的,
设置相对于超级视图的标签约束!
因此,不要将标签的约束相对于其周围的其他事物放置,而是将其约束到表视图单元格的内容视图。
然后,确保标签的高度设置为大于或等于 0,并且行数设置为 0。
然后在ViewDidLoad添加:
tableView.estimatedRowHeight = 695
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)
小智 5
就我而言 - 在故事板中,我有两个标签,如下图所示, 两个标签在我使其相等之前都设置了所需的宽度值。一旦您取消选择,它将更改为自动,并且像往常一样,拥有以下内容应该像魅力一样工作。
1.rowHeight = UITableView.automaticDimension, and
2.estimatedRowHeight = 100(In my case).
3.make sure label number of lines is zero.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101414 次 |
| 最近记录: |