xcode 如何将 UITableView 静态单元格设置为自定义高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11172827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a UITableView static cell to a custom height?
提问by user1282180
I have a UITableView with static cells and I am trying to change the height of the cell when it meets a condition. The conditions are being met but the cell heights won't change.
我有一个带有静态单元格的 UITableView,我试图在它满足条件时更改单元格的高度。条件得到满足,但单元格高度不会改变。
The code I am using is
我使用的代码是
[self.tableView setRowHeight:10];
And
和
self.tableView rowHeight = 10;
Neither of these will change the row height of the cells Is there another way to change them?
这些都不会改变单元格的行高有没有另一种方法来改变它们?
回答by JulenissensHjelper
You need to implement the following delegate-method in your tableview's .m:
您需要在 tableview 的 .m 中实现以下委托方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return customTableCellHeight;
}
And then change the value of customTableCellHeight:
然后更改 customTableCellHeight 的值:
if (condition) {
customTableCellHeight = 80; // or whatever
}
When your table cell height needs to change (like when the condition changes, you need to call
当你的表格单元格高度需要改变时(比如条件改变时,你需要调用
[self.tableView reloadData]; //This calls heightForRowAtIndexPath for all cells
回答by TeckPress.Net
you can use this for all row of tableview
您可以将其用于 tableview 的所有行
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
回答by Anit Kumar
You Can create the dynamic cell with Text height then you can set the static and dynamic cell both in on table view
您可以创建具有文本高度的动态单元格,然后您可以在表格视图中设置静态和动态单元格
Here is link to dynamic cell with text How to change cell height dynamically in UITableView static cell
这是带有文本的动态单元格的链接 如何在 UITableView 静态单元格中动态更改单元格高度