如何在TableView中更改Xcode中原型单元格的高度?

时间:2022-11-21 21:02:13

I have dragged a UITableView into my storyboard and I can fully insert info into it. But in order to customise the cell I added a prototype cell. When I change the height of it manually nothing changes in the simulator.

我已将UITableView拖入我的故事板,我可以完全插入信息。但为了定制单元格,我添加了原型单元格。当我手动更改它的高度时,模拟器中没有任何变化。

如何在TableView中更改Xcode中原型单元格的高度?

3 个解决方案

#1


5  

You can change the height of a cell by adding Constraints to your label inside the cell or otherwise by adding this to your viewDidLoad:

您可以通过向单元格内的标签添加约束来更改单元格的高度,或者通过将其添加到viewDidLoad来更改单元格的高度:

tableView.rowHeight = 40

Or you may add the following delegate function to change the height:

或者您可以添加以下委托函数来更改高度:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40
}

#2


3  

For those who come here just wanting to change the row height during design time, you can select the Table View Cell and then set the row height in the Size inspector.

对于那些只是想在设计时更改行高的人来说,可以选择“表视图单元”,然后在“大小”检查器中设置行高。

如何在TableView中更改Xcode中原型单元格的高度?

I'm adding this answer because I found this question when I was having trouble manually dragging the cell height to something else. As the OP noted, though, this will not change the cell height at run time. The cell is auto-sized depending on its content. If you need to give it a fixed height, see the accepted answer.

我正在添加这个答案,因为当我无法手动将单元格高度拖动到其他位置时,我发现了这个问题。但是,正如OP指出的那样,这不会改变运行时的单元高度。单元格根据其内容自动调整大小。如果您需要给它一个固定的高度,请参阅接受的答案。

#3


1  

You can use tableView delegate method and specify the value for your heightForRowAt

您可以使用tableView委托方法并指定heightForRowAt的值

say you want it to be 80:

说你希望它是80:

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat(80)
    }

#1


5  

You can change the height of a cell by adding Constraints to your label inside the cell or otherwise by adding this to your viewDidLoad:

您可以通过向单元格内的标签添加约束来更改单元格的高度,或者通过将其添加到viewDidLoad来更改单元格的高度:

tableView.rowHeight = 40

Or you may add the following delegate function to change the height:

或者您可以添加以下委托函数来更改高度:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40
}

#2


3  

For those who come here just wanting to change the row height during design time, you can select the Table View Cell and then set the row height in the Size inspector.

对于那些只是想在设计时更改行高的人来说,可以选择“表视图单元”,然后在“大小”检查器中设置行高。

如何在TableView中更改Xcode中原型单元格的高度?

I'm adding this answer because I found this question when I was having trouble manually dragging the cell height to something else. As the OP noted, though, this will not change the cell height at run time. The cell is auto-sized depending on its content. If you need to give it a fixed height, see the accepted answer.

我正在添加这个答案,因为当我无法手动将单元格高度拖动到其他位置时,我发现了这个问题。但是,正如OP指出的那样,这不会改变运行时的单元高度。单元格根据其内容自动调整大小。如果您需要给它一个固定的高度,请参阅接受的答案。

#3


1  

You can use tableView delegate method and specify the value for your heightForRowAt

您可以使用tableView委托方法并指定heightForRowAt的值

say you want it to be 80:

说你希望它是80:

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat(80)
    }