Ios tableView未设置自动行高
在我的项目中,我有一个包含3个部分的静态表视图。第二部分中的单元格包含动态填充的标签,因此具有动态高度。单元格应根据标签的高度调整其高度。以下是我尝试过的,但没有成功:Ios tableView未设置自动行高,ios,swift,tableview,row-height,Ios,Swift,Tableview,Row Height,在我的项目中,我有一个包含3个部分的静态表视图。第二部分中的单元格包含动态填充的标签,因此具有动态高度。单元格应根据标签的高度调整其高度。以下是我尝试过的,但没有成功: override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if indexPath.section == 0 { return 44 }
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 {
return 44
} else if indexPath.section == 1 {
return UITableViewAutomaticDimension
} else if indexPath.section == 2 {
return 80
} else {
return 50
}
}
除自动尺寸标注外,所有截面的高度均已正确设置。有什么帮助吗?您还需要提供估计的行高。您可以通过使用
UITableView
的estimateDroweight
属性,或者实现相应的委托方法来实现这一点:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return XXXXX // Provide your estimation here, or pass UITableViewAutomaticDimension (not the best for performances)
}
参考:您还需要提供估计的行高。您可以通过使用
UITableView
的estimateDroweight
属性,或者实现相应的委托方法来实现这一点:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return XXXXX // Provide your estimation here, or pass UITableViewAutomaticDimension (not the best for performances)
}
参考:在
viewDidLoad()中设置此行
然后编写这个表视图
方法
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
还要确保正确使用了自动布局
。
您已经设置了标签的行数=0
在viewDidLoad()中设置这一行
然后编写这个表视图
方法
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
还要确保正确使用了自动布局
。
您已经为lable=0设置了行数
更多信息将非常有用-您是否为单元格设置了所有约束条件?您是否正确设置了标签(即numberOfLines=0,lineBreakMode)?我将标签的约束设置为左10、上10和右10。标签有0条线和默认的lineBreakMode(截断尾部)。这里有什么要更改的吗?是的,你想换行模式为word wrap!0行是正确的,您需要有多行标签。更多信息将非常有用-您是否为单元格设置了所有约束?您是否正确设置了标签(即numberOfLines=0,lineBreakMode)?我将标签的约束设置为左10、上10和右10。标签有0条线和默认的lineBreakMode(截断尾部)。这里有什么要更改的吗?是的,你想换行模式为word wrap!0行是正确的,您需要有多行标签。不幸的是,这不起作用,可能是自动布局的问题?我只为单元格内的标签设置了约束(左10,上10,右10),并将线条设置为0。plz也给出了底部约束。其他人都这样做了。谢谢欢迎不要犹豫,给它打分不幸的是,这不起作用,也许是自动布局的问题?我只为单元格内的标签设置了约束(左10,上10,右10),并将线条设置为0。plz也给出了底部约束。其他人都这样做了。谢谢欢迎别犹豫,给自己打分