如何动态调整自定义单元格高度

问题描述 投票:0回答:5

我想动态调整自定义单元格高度。

在我的 tableView 中有三个自定义列:

  1. 产品名称
  2. 产品数量
  3. 产品价格

现在产品名称栏不显示全名。它只显示适合的文本。但无法显示全名。

虽然我已设置

heightForRowAtIndexPath
如下:

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

    return UITableViewAutomaticDimension;
}

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

下面的行是在我的

CustomCell
类中设置的

self.productNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.productNameLabel.numberOfLines = 0; 

示例:

NSString *prodName = @"This is my product name which may contain description with it";

因此,在上面的示例中,我的

UITableViewCell
仅显示了这么多产品名称:
This is my product
,并且它转义了更多字符串。

请参阅下面附加的属性部分和约束...

我想在

Nutrient
栏设置产品名称

请帮助我,尝试了所有可能的解决方案,但无法解决这个问题。

这样我的 uitableview 数据就会显示...但无法显示营养成分中的全名。

ios objective-c uitableview row-height
5个回答
3
投票

首先,

estimatedRowHeight
应设置为某个特定数字。这是
tableView
用于计算滚动指示器大小的估计,但并不真正使用它来调整单元格大小。所以理想情况下你应该设置类似的东西:

tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension

然后确保您的单元格已定义可用于定义其大小(特别是高度)的自动布局约束。在您的情况下,请确保标签顶部锚点约束到单元格的

contentView.topAnchor
,底部锚点约束到单元格的
contentView.bottomAnchor
,并将标签的前导和尾随约束到
contentView
的前导和尾随。因此,当标签有更多内容时,它会尝试扩展,单元格也应该如此。

考虑使用

UIStackView
来管理单元格的内容,因为这样即使单元格的展开和折叠也会更容易管理。如果这是您的目标,请参阅我对另一个问题的回答


0
投票

在尺寸检查器中为标签设置这些值(如图所示),并且不要为标签设置高度限制。


0
投票

你已经做得很好,但还需要更多。首先你设置。 表视图.estimatedRowHeight = 44 在给定等于 13 的底部约束之后,只需将其设置为大于或等于 13。这对您有用。


0
投票

要设置行高和估计行高的自动尺寸,请确保执行以下步骤以使自动尺寸对单元格/行高布局有效。我刚刚测试了以下步骤和代码并且工作正常。

  • 分配并实现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
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension
}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

对于 UITableviewCell 中的标签实例

  • 设置行数= 0(&换行模式=截断尾部)
  • 设置相对于其超级视图/单元容器的所有约束(顶部、底部、右左)。
  • 可选:设置标签的最小高度,如果您希望标签覆盖最小垂直区域,即使没有数据。

enter image description here

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


0
投票

另一种方法:

  • 将标签(或多个标签)放入堆栈视图中,并将堆栈视图固定到内容视图的边缘。
© www.soinside.com 2019 - 2024. All rights reserved.