动态增加UILabel和TableView Cell的高度?

Dhe*_*mar 15 objective-c row-height uitableview ios swift

我有一个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;
}
Run Code Online (Sandbox Code Playgroud)

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

Bha*_*odi 34

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

另一个约束如下,

ItemLabel:

在此输入图像描述

SeparatorView:

在此输入图像描述

DescriptionLabel:

在此输入图像描述

并为高度添加代表,如下所示,

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

    return 44.0;
}

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

    return UITableViewAutomaticDimension;
}
Run Code Online (Sandbox Code Playgroud)

你应该得到如下输出,

在此输入图像描述

希望这会对你有所帮助.


Kru*_*nal 6

要设置行高和估计行高的自动尺寸,请确保执行以下步骤,自动尺寸对单元格/行高度布局有效.

  • 分配并实现tableview dataSource和delegate
  • 分配UITableViewAutomaticDimension给rowHeight和estimatedRowHeight
  • 实现delegate/dataSource方法(即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;
}
Run Code Online (Sandbox Code Playgroud)

迅速:

@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
}
Run Code Online (Sandbox Code Playgroud)

对于UITableviewCell中的标签实例

  • 设置行数= 0(&换行模式=截断尾部)
  • 设置相对于其superview/cell容器的所有约束(顶部,底部,右侧).
  • 可选:如果您希望标签覆盖最小垂直区域,即使没有数据,也可以设置标签的最小高度.

在此输入图像描述

注意:如果您有多个具有动态长度的标签(UIElements),应根据其内容大小进行调整:针对要以更高优先级展开/压缩的标签调整"内容拥抱和压缩阻力优先级".