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

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

问题描述

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





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



项目标签约束





中心视图约束





右视图限制





我正在使用一个bean类,用于存储两个标签和数据的数据。将该bean对象添加到一个数组中。我在heightForRowAtIndexPath中使用下面的代码。

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

//基于单元格计算高度
if(!self.customCell){
self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@ thirdcell];
}

//配置单元格
Instrument * inst = [arr_instSpecs objectAtIndex:indexPath.row];
self.customCell.label_item.text = inst.item;
self.customCell.label_desc.text = inst.desc;


//布局单元格

[self.customCell layoutIfNeeded];

//获取单元格的高度

CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] .height;

//填充1点(单元格分隔符)
CGFloat separatorHeight = 1;

返回高度+ separatorHeight;
}

问题既不是标签的高度增加也不是表视图单元格。我有解释了一切。当标签的文字和文字增加时,我想增加标签的大小。当标签尺寸增加时,单元格的高度必须增加。

解决方案

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



其他约束如下,



ItemLabel:





SeparatorView:





DescriptionLabel:





希望这会对您有所帮助。


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

Item label constraints

center view constraints

right view constarints


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.

解决方案

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.

And the other constraint are as below,

ItemLabel:

SeparatorView:

DescriptionLabel:

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

You should get the output as below,

Hope this would help you.

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

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