ios 更改 UITableViewCell 的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8543846/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:55:38  来源:igfitidea点击:

Change Height of a UITableViewCell

iosobjective-cuitableview

提问by user973985

I want to make a UITableViewCellbigger. How can I do this, I have tried adjusting its frame but nothing happens. What can I do?

我想做一个UITableViewCell更大的。我该怎么做,我试过调整它的框架,但没有任何反应。我能做什么?

回答by Shane Powell

You need to implement the

你需要实现

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

method in your UITableViewDelegate.

UITableViewDelegate 中的方法。

回答by User-1070892

You can do like this..

你可以这样做..

//Change the Height of the Cell [Default is 44]:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
  if (indexPath.section == 0) {
   if (indexPath.row == 1) {
     return 90;
   }
  }
  return 44;
}

I hope this will help you..

我希望这能帮到您..

回答by scaryguy

There are 2 steps to change height of a table cell withoutany additional code:

无需任何附加代码即可更改表格单元格的高度有 2 个步骤:

  1. Select your Table Viewfrom your Storyboard. In the Size Inspectorchange Row Height.
  2. Select your Table View Celland while it's selected in the Size Inspectorchange the Row Height.
  1. Table View从您的故事板中选择您的。在Size Inspector变化中Row Height
  2. 选择您的Table View Cell和 在Size Inspector更改Row Height.

Note that you MUSTset BOTHof Table View's Row Heightattribute and Table View Cell's Row Heightattribute.

请注意,您必须设置BOTH表视图的的Row Height属性和表格视图单元格的Row Height属性。

If you just set one of them, it won't work.

如果您只设置其中之一,它将不起作用。

回答by ChrisJF

I found this answerto be the most informative and most up-to-date to change the height of a table view cell.

我发现这个答案是更改表格视图单元格高度的最有用和最新的答案

TL;DR

TL; 博士

  1. Take extra care when making your Auto Layout constraints
  2. Enable row height estimation on your table view in the viewDidLayoutSubviewsmethod.

    self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is

  1. 制作自动布局约束时要格外小心
  2. viewDidLayoutSubviews方法中在您的表视图上启用行高估计。

    self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.estimatedRowHeight = 44.0; // 设置为您的“平均”单元格高度

回答by Nithinbemitk

You can use this code for changing the height of different tableview

您可以使用此代码更改不同 tableview 的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == tableView1) 
    {
        return 55;
    }
    else if(tableView == tableView2)
    {
        return 75;
    }
}

This may help you.

这可能对你有帮助。