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 如何在Swift中更改UITableView单元格的高度?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何在Swift中更改UITableView单元格的高度?

Ios 如何在Swift中更改UITableView单元格的高度?,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试对一个应用程序显示评论,如下图所示。我使用的是Xcode 9.4.1 我使用以下代码隐藏标签: if commentManager == nil { cell.managerReplyLabel.isHidden = true cell.ratingManagerLabel.isHidden = true } 但它看起来仍然有一个空间,我隐藏了两个标签 我尝试使用cell.ratingManagerLabel.font.with size(0)或 cell.rati

我正在尝试对一个应用程序显示评论,如下图所示。我使用的是Xcode 9.4.1

我使用以下代码隐藏标签:

if commentManager == nil {
    cell.managerReplyLabel.isHidden = true
    cell.ratingManagerLabel.isHidden = true
}
但它看起来仍然有一个空间,我隐藏了两个标签

我尝试使用
cell.ratingManagerLabel.font.with size(0)

cell.ratingManagerLabel.frame.size.height=0
更改标签高度,但它不起作用

如何减少空间

更新:

我使用故事板设置屏幕和自动布局


在UITableView类中使用委托方法

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return 200 //or whatever you need
}

有两种简单的解决方案:

  • 将两个标签上的文本设置为
    nil
    (或
    “”
    )。然后标签的高度为零

  • 以垂直布局方向将标签包装到视图中。然后设置
    .ishiden=true
    也将有效地删除标签

  • 还请注意,您可能需要调用:

    tableView.beginUpdates()
    tableView.endUpdates()
    

    强制表格更新其单元格的高度。

    使用此tableview方法增加和减少行的高度

    var selectedIndex = -1
    
     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if indexPath.row == selectedIndex {
                return 354
            }else {
                return 115
            }
        }
    

    显示tableViewCell代码。是否要降低单元格高度?我使用情节提要进行设置和更新。使用自调整大小的tableviewUsing
    UIStackView
    是最简单的解决方案。谢谢您的回答。此方法将更改表视图单元格的所有大小。但是我只想在某些情况下更改高度。您需要使用
    switch
    if…else
    语句来针对要更改高度的行/单元格。它适用于数字1,很抱歉,您能否提供一个关于数字2的简单代码?谢谢@Peggy第2条是什么?如何使用“将标签包装到具有垂直布局方向的UIStackView中。然后设置.isHidden=true将有效地删除标签。”?@Peggy将
    UIStackView
    添加到故事板中,并将标签放入其中。没有涉及任何代码。哦!抱歉,我没有意识到这是故事板中的垂直堆栈视图。这个解决方案看起来不错。谢谢
    var selectedIndex = -1
    
     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if indexPath.row == selectedIndex {
                return 354
            }else {
                return 115
            }
        }