【问题标题】:Get dynamic height of UITableViewCell获取 UITableViewCell 的动态高度
【发布时间】:2017-01-17 09:03:47
【问题描述】:

我在UITableViewCell 中使用UITableView。 我希望单元格根据 tableView 的内容大小调整大小。

我试图通过

获得每个单元格的高度
 CGRect frame = [self.Table rectForRowAtIndexPath:indexPath];

但它没有返回单元格的正确高度。这些单元格根据文本获得动态高度。

【问题讨论】:

  • 我的单元格正在获得动态高度,我希望我的表格视图根据单元格的大小调整大小,因为我想禁用表格视图的滚动。
  • @Ahmad 你的问题的很多答案已经存在于堆栈溢出中,并且是 Arun 给出的最佳建议答案之一,请遵循它。
  • @the_dahiya_boy 我的问题与此链接相同link

标签: ios objective-c uitableview


【解决方案1】:

在 viewDidLoad 方法中

self.table.estimatedRowHeight = 44.0 ;
self.table.rowHeight = UITableViewAutomaticDimension;

//numberOfLinesOfYourLabel should be 1

然后

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension
}

【讨论】:

    【解决方案2】:
    -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
       if(tableView == MainTableView){
     //Calculate height for cell for perticular indexpath of main Tableview
       }
       else if(tableView == subTableView){
      //Calculate height for cell for perticular indexpath of main Tableview  } }
    
    • 在 mainTableview 的 cellForRow 方法中重新加载子表视图。

    【讨论】:

    • 我的单元格根据文本获得动态高度,设置高度不是一个选项,因为我使用 4 种不同类型的单元格,每个单元格有 2-3 个不同字体和字体大小的标签。
    • 是的,你必须根据 heightForRow 方法中的文本和字体计算单元格的高度
    猜你喜欢
    • 2016-05-05
    • 2013-07-29
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2011-06-20
    • 1970-01-01
    相关资源
    最近更新 更多