【问题标题】:How to resize UITableView height according to the height of UITableViewCells?如何根据 UITableViewCells 的高度调整 UITableView 的高度?
【发布时间】:2016-05-16 09:28:50
【问题描述】:

我有一个表格视图,我为单元格设置了自动布局,所以一个单元格的高度可能是 50,另一个可能是 120。我需要显示最多 2 个单元格,所以如果计数 > 2,那么单元格计数 = 2,否则它等于细胞数。

我的问题是根据每个单元格大小调整 UITableView 的高度。如何正确设置 UITableView 高度约束值?

我希望我能解释一下我想要什么,如果你有任何问题,请问我

【问题讨论】:

  • 是文本内容动态
  • 加载您的表格视图数据并使用 tableview.layoutIfNeeded 强制布局。然后你可以设置:tableviewHeightContraint.constant = tableview.contentSize.height

标签: ios swift uitableview autolayout


【解决方案1】:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == 0 { 
        return 50 
    } else { 
        return 120 
    } 
}

【讨论】:

    【解决方案2】:

    不要明确设置高度。使用这个:

    tableView.estimatedRowHeight = 100
    tableView.rowHeight = UITableViewAutomaticDimension
    

    【讨论】:

    • 这是为了调整单元格高度而不是表格视图高度
    • 您是指表格视图内容大小吗?使用自动布局时,tableview 内容大小会自动更新。
    • 没有。我的意思是说表格视图高度
    • 然后可以给tableview height = tableview内容大小。
    【解决方案3】:

    看看我在这篇 StackOverflow 文章中发布的代码。

    Dynamic height

    您只需向您的UITableViewCell 添加一个静态函数,该函数“测量自身”,然后将其存储在“行高”变量中。

    +(CGSize)preferredSize
    {
        static NSValue *sizeBox = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            // Assumption: The XIB file name matches this UIView subclass name.
            NSString* nibName = NSStringFromClass(self);
            UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
    
            // Assumption: The XIB file only contains a single root UIView.
            UIView *rootView = [[nib instantiateWithOwner:nil options:nil] lastObject];
    
            sizeBox = [NSValue valueWithCGSize:rootView.frame.size];
        });
        return [sizeBox CGSizeValue];
    }
    

    (你会认为到现在,在 2016 年,Apple 已经为我们提供了一种更简单的方法......)

    【讨论】:

      【解决方案4】:

      在计算高度时尝试将 UITableViewCell 高度存储在数组中。

      如果您手动计算高度,请将高度存储在数组中。 如果您要自动调整单元格高度,请获取单元格,然后获取其高度并将其存储在数组中

      【讨论】:

        【解决方案5】:

        如果您的 ios 目标 >= 8,那么您可以轻松调整表格视图单元格。

        //provide the estimated height of your cell row over here
        self.tableView.estimatedRowHeight = 60
        self.tableView.rowHeight = UITableViewAutomaticDimension
        

        不要忘记在单元格的内容视图中为所有视图设置上、下、左、右约束。

        【讨论】:

          【解决方案6】:

          试试下面的代码吧

          func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
              return UITableViewAutomaticDimension
          }
          
          func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
              return UITableViewAutomaticDimension
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-07-09
            • 2013-10-01
            • 2012-02-27
            • 2022-01-22
            • 2017-12-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多