Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 某些tableview单元格具有动态高度,另一些具有固定高度_Ios_Swift_Uitableview_Tableview_Row Height - Fatal编程技术网

Ios 某些tableview单元格具有动态高度,另一些具有固定高度

Ios 某些tableview单元格具有动态高度,另一些具有固定高度,ios,swift,uitableview,tableview,row-height,Ios,Swift,Uitableview,Tableview,Row Height,我有一个桌面视图。我想要一些具有动态高度的tableview单元格,而其他单元格具有固定高度。对于动态高度,我在viewDidLoad视图控制器委托中使用了以下几行 tableView.estimatedRowHeight = 200 tableView.rowHeight = UITableViewAutomaticDimension 在一个tableview中使用固定高度单元格和自调整大小单元格的有效方法是什么 提前感谢。Swift 4 您可以使用此函数中的indexPath,为某些行返回

我有一个桌面视图。我想要一些具有动态高度的tableview单元格,而其他单元格具有固定高度。对于动态高度,我在viewDidLoad视图控制器委托中使用了以下几行

tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
在一个tableview中使用固定高度单元格和自调整大小单元格的有效方法是什么


提前感谢。

Swift 4

您可以使用此函数中的
indexPath
,为某些行返回固定高度,为其他行返回动态高度

// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Swift 3

通过执行此操作,可以在tableview中重新运行动态和静态单元格高度

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
    }
}

不要忘记将标签行号设置为0&lineBreakMode,该标签将动态展开&最重要的是不要将标签高度设置为固定高度。

假设indexath.row=0和1分别包含静态高度和动态高度,然后

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
{
     if indexPath.row == 0 {
        return 120
     } else {
        return UITableViewAutomaticDimension
     }
}

是否必须实现委托方法“estimatedHeightForRowAt”?首先,我在没有它的情况下进行了尝试&结果并不是我所期望的。我的意思是细胞高度根本没有扩大。后来我尝试了一下,得到了我想要的结果。我面临着同样的问题&我用这种方式解决了它。不过,这是一个太晚的回应。你找到更好的解决办法了吗?如果有,请随意分享。好的,谢谢您的回复!我会尝试一下,并让您知道:)Swift 5 UITableView.automaticDimension