【问题标题】:Dynamically increase height of UILabel & TableView Cell?动态增加 UILabel & TableView Cell 的高度?
【发布时间】:2023-03-28 14:15:01
【问题描述】:

我有一个 UITableView,我在其中显示一个自定义单元格。我的单元格有两个标签和一个视图,如下图所示。

我已经给了这样的左视图约束

项目标签约束

中心视图约束

右视图约束


我正在使用 bean 类来存储两个标签的数据并将该 bean 对象添加到一个数组中。我在 heightForRowAtIndexPath 中使用以下代码。

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

    // Calculate a height based on a cell
    if(!self.customCell) {
        self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"thirdcell"];
    }

    // Configure the cell
    Instrument *inst=[arr_instSpecs objectAtIndex:indexPath.row];
    self.customCell.label_item.text=inst.item;
    self.customCell.label_desc.text=inst.desc;


    // Layout the cell

    [self.customCell layoutIfNeeded];

    // Get the height for the cell

    CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    // Padding of 1 point (cell separator)
    CGFloat separatorHeight = 1;

    return height + separatorHeight;
}

问题既不是标签的高度增加,也不是表格视图单元格的高度。我已经解释了一切。我想在标签文本增加时增加标签大小,并且当标签大小增加时,单元格的高度必须增加。

【问题讨论】:

  • 您的问题不清楚您想要实现什么。仍然从您的代码中,您通过计算返回高度而不是返回 UITableViewAutomaticDimension。
  • 关于 SO 上的类似场景有很多答案,这是stackoverflow.com/questions/19215199/…之一
  • 我已经解释了一切。我想让标签的大小在标签文本增加时增加,并且当标签大小增加时,单元格的高度必须增加。
  • 'Item' 和 'Description' 都是 UILabel 类吗?您的单元格中是否只有两个标签?
  • 您提到您在单元格中也有一个视图,它在哪里?它的用途是什么?

标签: ios objective-c swift uitableview row-height


【解决方案1】:

首先,您不应该在自动布局环境中手动计算高度。只需将两个标签 TopSpace 和 BottomSpace 设置为单元格的 contentView,并确保将两个标签 NumberOfLines 设置为 0 并将 LineBreakMode 设置为 WordWrap。

其他约束如下,

物品标签:

SeparatorView:

描述标签:

并添加高度代表如下,

#pragma mark - UITableView Delegates
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44.0;
}

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

    return UITableViewAutomaticDimension;
}

你应该得到如下输出,

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    要为行高和估计行高设置自动尺寸,请确保按照以下步骤制作,自动尺寸对单元格/行高布局有效。

    • 分配和实现 tableview 数据源和委托
    • UITableViewAutomaticDimension 分配给rowHeight 和estimatedRowHeight
    • 实现委托/数据源方法(即heightForRowAt并返回一个值UITableViewAutomaticDimension给它)

    -

    目标 C:

    // in ViewController.h
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
    
      @property IBOutlet UITableView * table;
    
    @end
    
    // in ViewController.m
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.table.dataSource = self;
        self.table.delegate = self;
    
        self.table.rowHeight = UITableViewAutomaticDimension;
        self.table.estimatedRowHeight = UITableViewAutomaticDimension;
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        return UITableViewAutomaticDimension;
    }
    

    斯威夫特:

    @IBOutlet weak var table: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Don't forget to set dataSource and delegate for table
        table.dataSource = self
        table.delegate = self
    
       // Set automatic dimensions for row height
        // Swift 4.2 onwards
        table.rowHeight = UITableView.automaticDimension
        table.estimatedRowHeight = UITableView.automaticDimension
    
    
        // Swift 4.1 and below
        table.rowHeight = UITableViewAutomaticDimension
        table.estimatedRowHeight = UITableViewAutomaticDimension
    
    }
    
    
    
    // UITableViewAutomaticDimension calculates height of label contents/text
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // Swift 4.2 onwards
        return UITableView.automaticDimension
    
        // Swift 4.1 and below
        return UITableViewAutomaticDimension
    }
    

    对于 UITableviewCell 中的标签实例

    • 设置行数=0(&换行模式=截尾)
    • 设置与其父视图/单元格容器相关的所有约束(上、下、左右)。
    • 可选:设置标签的最小高度,如果你想要标签覆盖的最小垂直区域,即使没有数据。

    注意:如果您有多个标签(UIElements)具有动态长度,应根据其内容大小进行调整:为您的标签调整'Content Hugging and Compression Resistance Priority'想要以更高的优先级展开/压缩。

    【讨论】:

    • 我有带有掩码约束的旧 xib...您知道如何为带有掩码约束的 xib 执行此操作吗?
    【解决方案3】:

    您需要在视图加载时定义最大估计高度

    tblObject.estimatedRowHeight = 300;
    tblObject.rowHeight = UITableViewAutomaticDimension;
    

    【讨论】:

      【解决方案4】:

      这就是我的工作方式。

      1) 设置编号。行数为 0。

      2) 设置换行符。

      3) 添加约束。

      4) 更改垂直内容拥抱优先级。

      5) 在 ViewDidLoad 上:

      override func viewDidLoad() {
          super.viewDidLoad()
      
          self.sampleTableView.estimatedRowHeight = 600.0;
          self.sampleTableView.rowHeight = UITableViewAutomaticDimension;
      
          self.sampleTableView.setNeedsLayout()
          self.sampleTableView.layoutIfNeeded()
      }
      

      6) 在 TableView 自定义单元格上:

      override func awakeFromNib() {
          super.awakeFromNib()
          // Initialization code
      
          sizeToFit()
          layoutIfNeeded()
      }
      

      【讨论】:

        【解决方案5】:

        将您的标签约束设置为顶部到单元格,底部到单元格并保持您的前导和尾随约束不变。 在

        - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
           return UITableViewAutomaticDimension;
        }
        
        -(CGFloat)tableView:(UITableView *)tableView
            estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
            return UITableViewAutomaticDimension;
        }
        

        通过使用 UITableViewAutomaticDimension 将根据您的标签内容增加您的表格视图单元格高度,最重要的一点是,选择您的标签,然后转到身份检查器并在行数中写入 0

        【讨论】:

          【解决方案6】:

          斯威夫特 3 / 斯威夫特 4

          自定义单元格:

          class CustomCell: UITableViewCell {
          
              @IBOutlet var messageLabel: UILabel!
          
              override func awakeFromNib() {
                  super.awakeFromNib()
          
                  sizeToFit()
                  layoutIfNeeded()
          
              }
          
          }
          

          viewDidLoad:

          override func viewDidLoad() {
              super.viewDidLoad()        
          
              self.tableView.estimatedRowHeight = 80
              self.tableView.rowHeight = UITableViewAutomaticDimension
          
          }
          

          还有 cellForRowAtIndexPath:

          public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          
               let cell : CustomCell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifier", for: indexPath) as! CustomCell
               cell.messageLabel.text = exampleContent[indexPath.row]
               cell.messageLabel.sizeToFit()
               cell.messageLabel.layoutIfNeeded()
               return cell
          

          }

          【讨论】:

            【解决方案7】:

            斯威夫特 4.2

            对于BasicSubtitle 类型的单元格,首先在viewDidLoad 中执行以下操作:

                self.tableView.estimatedRowHeight = 44 // This is the default cell height
                self.tableView.rowHeight = UITableView.automaticDimension
            

            然后选择标题并将其行数设置为 0。现在行将根据标题文本调整其高度。你不需要做任何其他事情。

            您的自定义单元格类型也是如此。确保您在自定义单元格中使用的标签也设置为 0 行。

            【讨论】:

              【解决方案8】:

              我正在编写其他人的代码,但给出的答案均不适合我。约束是正确的。 将标签的“所需宽度”更改为自动,在情节提要中为我完成了这项工作。为此,请取消选中标签大小检查器中的“显式”复选标记。

              【讨论】:

                猜你喜欢
                • 2016-06-17
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多