Ios 动态增加UILabel的高度&;TableView单元?

Ios 动态增加UILabel的高度&;TableView单元?,ios,objective-c,swift,uitableview,row-height,Ios,Objective C,Swift,Uitableview,Row Height,我有一个UITableView,其中显示一个自定义单元格。我有两个标签和一个视图,如下图所示 我已经给出了这样的左视图约束 项目标签约束 中心视图约束 右派警察 我使用一个bean类来存储两个标签的数据&将该bean对象添加到一个数组中 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // Calculate a height bas

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

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

项目标签约束

中心视图约束

右派警察


我使用一个bean类来存储两个标签的数据&将该bean对象添加到一个数组中

- (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,将根据标签的内容增加tableview单元格的高度,最重要的一点是,选择标签,然后转到Identity Inspector,在行数中写入0

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

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

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

另一个约束条件如下:

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

    return 44.0;
}

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

    return UITableViewAutomaticDimension;
}
项目标签:

// 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
}

分离器视图:

// 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
}

说明标签:

// 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
}

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

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

    return 44.0;
}

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

    return UITableViewAutomaticDimension;
}
您应该得到如下输出:

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

    return 44.0;
}

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

    return UITableViewAutomaticDimension;
}


希望这能对你有所帮助。

我就是这样做的

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()
}
override func viewDidLoad() {
    super.viewDidLoad()        

    self.tableView.estimatedRowHeight = 80
    self.tableView.rowHeight = UITableViewAutomaticDimension

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

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    sizeToFit()
    layoutIfNeeded()
}

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

  • 分配并实现tableview数据源和委托
  • UITableViewAutomaticDimension
    分配给行高和估计高度
  • 实施委托/数据源方法(即
    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(&换行模式=截断尾部)
  • 设置与其superview/单元容器相关的所有约束(顶部、底部、右-左)
  • 可选:设置标签的最小高度,如果您想要标签覆盖的最小垂直面积,即使没有数据

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

Swift 3/Swift 4

自定义单元格:

class CustomCell: UITableViewCell {

    @IBOutlet var messageLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

        sizeToFit()
        layoutIfNeeded()

    }

}
viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    self.sampleTableView.estimatedRowHeight = 600.0;
    self.sampleTableView.rowHeight = UITableViewAutomaticDimension;

    self.sampleTableView.setNeedsLayout()
    self.sampleTableView.layoutIfNeeded()
}
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

}Swift 4.2

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

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

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


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

我正在处理其他人的代码,但给出的答案都不适用于我。约束条件是正确的。 在故事板中将标签的“所需宽度”更改为“自动”,我就完成了这项工作。为此,请取消选中标签尺寸检查器中的“显式”复选标记


你的问题不清楚你想要实现什么。仍然从代码中,您通过计算返回高度,而不是返回UITableViewAutomaticDimension。关于类似场景,有很多答案,这是我解释过的所有内容中的一个。当标签文本增加时,我想使标签的大小增加,当标签大小增加时,单元格的高度必须增加。“Item”和“Description”都是UILabel类吗?你的单元格中只有两个标签吗?你提到过你在单元格中也有一个视图,它在哪里?它有什么用呢?我有带掩码约束的旧XIB…你知道如何对带掩码约束的XIB执行此操作吗?