动态增加 UILabel & 的高度TableView 单元格? [英] Dynamically increase height of UILabel & TableView Cell?

查看:32
本文介绍了动态增加 UILabel & 的高度TableView 单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableView in which i am displaying a custom cell.I my cell i have two label & one view as below in picture.

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

I have given constraint of left view like this

项目标签限制

中心视图约束

右视图约束


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


I am using a bean class to store data for two labels & add that bean object into one array.I am using below code in 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;
}

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

Problem is neither height of label is increasing nor of table view cell.I have explained everything. I want to make size of label increase when there is increase in label's text & also when label size increase then height of cell must increase.

推荐答案

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

First of all you should not calculate height manually in auto layout environment. Just set both labels TopSpace and BottomSpace to cell's contentView and make sure you set both labels NumberOfLines to 0 and LineBreakMode to WordWrap.

其他约束如下,

项目标签:

SeparatorView:

描述标签:

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

And add the delegates for height as below,

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

    return 44.0;
}

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

    return UITableViewAutomaticDimension;
}

你应该得到如下输出,

希望对您有所帮助.

这篇关于动态增加 UILabel & 的高度TableView 单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆