Ios 表格视图上动态单元格高度中的标签未正确显示文本

Ios 表格视图上动态单元格高度中的标签未正确显示文本,ios,objective-c,swift,uitableview,row-height,Ios,Objective C,Swift,Uitableview,Row Height,我试图让我的表视图单元格根据单元格内的内容调整其高度 因为单元格的高度不同,所以它似乎可以工作,但我在正确显示文本方面遇到了问题 表格视图和原型单元是用故事板创建的,我有一个自定义单元类链接到我的原型单元。在我的原型单元中,我有三个标签;标题标签、日期标签和包含动态文本数据的标签。在情节提要的属性检查器中,动态文本数据标签行号设置为0,换行符设置为“文字换行” 当我运行应用程序时,单元格高度都不同,因此我假设索引路径处的行高度有效: - (CGFloat) tableView:(UITableV

我试图让我的表视图单元格根据单元格内的内容调整其高度

因为单元格的高度不同,所以它似乎可以工作,但我在正确显示文本方面遇到了问题

表格视图和原型单元是用故事板创建的,我有一个自定义单元类链接到我的原型单元。在我的原型单元中,我有三个标签;标题标签、日期标签和包含动态文本数据的标签。在情节提要的属性检查器中,动态文本数据标签行号设置为0,换行符设置为“文字换行”

当我运行应用程序时,单元格高度都不同,因此我假设索引路径处的行高度有效:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Text data
    NSDictionary *cellOutputDict = [newsDataArray objectAtIndex:indexPath.row];
    NSString *text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"text"]];

    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:nil];

    CGFloat width = 280;
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;

    return size.height + 2 * 20; // size.height plus 2 times the desired margin
}
这是我在索引路径的行的单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NewsCell";
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // If the cell doesn't exists create it
    if (cell == nil) {
        cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    // Text data
    NSDictionary *cellOutputDict = [newsDataArray objectAtIndex:indexPath.row];

    cell.titleLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"title"]];
    cell.dateLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"date"]];
    cell.newsTextLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"text"]];

    return cell;
}
输出动态文本数据的cell.newsTextLabel.text将显示在单元格中,但仅显示第一行

我曾尝试过一些在iOS 6.1上显示动态单元格高度的示例,它们工作得很好,但在iOS 7上无法实现+

所以我希望有人能帮我,帮我找出我做错了什么

试试这个

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

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    float height = cell.frame.size.height;
    return height;
}
试试这个

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

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    float height = cell.frame.size.height;
    return height;
}

要使标签随单元格一起展开,您应该为其设置约束,将其绑定到单元格上方和下方的视图——在您的情况下,该约束看起来像是绑定到单元格底部的约束,另一个绑定到单元格上方的标签(带有“Titel…”)

若要使标签随单元格展开,应为其设置约束,使其与上方和下方的视图相关联——在您的情况下,该约束看起来像是单元格底部的约束,而与上方的标签(带有“Titel…”)相关联

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

label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByClipping;
  • 分配并实现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)具有动态长度,则应根据其内容大小进行调整:为要以更高优先级展开/压缩的标签调整“内容拥抱和抗压优先级”。

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

  • 分配并实现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)具有动态长度,则应根据其内容大小进行调整:对于您希望以更高优先级展开/压缩的标签,请调整“内容拥抱和抗压优先级”。

嗯,我不知道我在我的问题中是否已经说清楚了。我不认为单元格的高度是我的问题,或者我还不知道,因为标签还没有打印出全文,但是不同单元格的高度似乎不同。这就是我现在的问题……嗯,我不知道我在我的问题中是否已经说得很清楚了。我不认为单元格的高度是我的问题,或者我还不知道,因为标签还没有打印出全文,但是不同单元格的高度似乎不同。这就是我现在的问题…对于可变文本长度的标签有什么限制?它需要绑定到单元格的底部和上面的标签(没有固定的高度限制),因此它将随单元格一起展开。@rdelmar当然!哈哈,我完全忘了设置约束。谢谢你向我指出这一点!你愿意把你的帖子作为答案,这样我就可以接受它作为解决方案吗?再次感谢!请问,你能解释一下如何设置禁忌症吗?我也有同样的问题。你对可变文本长度的标签有什么限制?它需要绑定到单元格的底部和上面的标签(没有固定的高度限制),因此它将随单元格一起展开。@rdelmar当然!哈哈,我完全忘了设置约束。谢谢你向我指出这一点!你愿意把你的帖子作为答案,这样我就可以接受它作为解决方案吗?再次感谢!请问,你能解释一下如何设置禁忌症吗?我也有同样的问题。