一些具有动态高度的表格视图单元格,其他具有固定高度
Some tableview cells with dynamic height, other with fixed height
我有一个表格视图。我想要一些具有动态高度的 tableview 单元格,而其他具有固定高度的单元格。对于动态高度,我在 viewDidLoad 视图控制器委托中使用了以下几行。
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
在一个表格视图中使用固定高度的单元格和自动调整单元格的有效方法是什么?
提前致谢。
Swift 4
您可以使用此函数的 indexPath
来 return 某些行的固定高度和其他行的动态高度。
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Swift 3
您可以通过执行此操作在表格视图中重新运行动态和静态单元格高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 120.0 // for static cell height
}
else {
return UITableViewAutomaticDimension // for dynamic cell height
}
}
你必须像那样实现另一个 tableview estimatedHeightForRowAt 方法
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 120.0
}
else {
return UITableViewAutomaticDimension
}
}
不要忘记将标签 lineNumbers 设置为 0 & lineBreakMode 标签将动态扩展,最重要的是不要将标签高度设置为固定。
假设,indexPath.row = 0和1分别包含静态高度和动态高度,则
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
if indexPath.row == 0 {
return 120
} else {
return UITableViewAutomaticDimension
}
}
我有一个表格视图。我想要一些具有动态高度的 tableview 单元格,而其他具有固定高度的单元格。对于动态高度,我在 viewDidLoad 视图控制器委托中使用了以下几行。
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
在一个表格视图中使用固定高度的单元格和自动调整单元格的有效方法是什么?
提前致谢。
Swift 4
您可以使用此函数的 indexPath
来 return 某些行的固定高度和其他行的动态高度。
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Swift 3
您可以通过执行此操作在表格视图中重新运行动态和静态单元格高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 120.0 // for static cell height
}
else {
return UITableViewAutomaticDimension // for dynamic cell height
}
}
你必须像那样实现另一个 tableview estimatedHeightForRowAt 方法
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 120.0
}
else {
return UITableViewAutomaticDimension
}
}
不要忘记将标签 lineNumbers 设置为 0 & lineBreakMode 标签将动态扩展,最重要的是不要将标签高度设置为固定。
假设,indexPath.row = 0和1分别包含静态高度和动态高度,则
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
if indexPath.row == 0 {
return 120
} else {
return UITableViewAutomaticDimension
}
}