在TableView iOS中使用UILabel的单元格的动态高度 [英] Dynamic height of cell with UILabel in tableview ios

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

问题描述

我正在使用一个聊天应用程序,该应用程序显示用户的消息和图像.我有一个表格视图和一个自定义单元格.该单元格有一个UIlabel和UIImage.我尝试了多种方法来根据label的内容文本动态调整单元格的高度.我在情节提要中设置了 numberofLines = 0 ,并将单元格的高度设置为常数,以便可以容纳多行但是它没有显示多条线,至于高度,我使用了自动标注,但是效果也不好.我还使用了以下链接而且我正在使用类型为messageTableViewCell的自定义单元格,其内部具有标签属性.

I am working on a chat application which shows the message and image of user. I have a table view and a custom cell in it.The cell has a UIlabel and UIImage. I tried various methods to resize the cell height dynamically based on the content text of label .I have set the numberofLines=0 in the storyboard itself and set height of cell a constant so that multiple lines can fit in. But it does not show multiple lines and as for height I used auto dimension but it didn't work as well . I also used the following link And I am using a custom cell of type messageTableViewCell which has a property of label inside it.

这是快照:-

我在viewcontroller.m中的代码

My code in viewcontroller.m

- (void)viewDidLoad
{

   self.tableView.estimatedRowHeight = 100.0;

   self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     messageTableViewCell *cell = [self.messageTableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) 
    {
         cell = [[messageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    cell.messageLabel.text = [_messages objectAtIndex:indexPath.row];
    cell.messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.messageLabel.numberOfLines=0;
    [cell.messageLabel sizeToFit];
    return cell;
}

编辑

我没有在标签中插入文本视图,而是将其修改为:

Instead of label I inserted a textview in the cell and modified it to :

- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth,     MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth),   newSize.height);
    textView.frame = newFrame;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static messageTableViewCell *sizingCell = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sizingCell = [self.messageTableView  dequeueReusableCellWithIdentifier:@"cell"];
   });

    sizingCell.textMessage.text=_messages[indexPath.row];
    CGFloat fixedWidth = sizingCell.textMessage.frame.size.width;
    CGSize newSize = [sizingCell.textMessage     sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = sizingCell.textMessage.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    CGFloat height= newFrame.size.height;
    NSLog(@"%f is height ",height);
    return height+5.0f;
}

但是它也不起作用,它切断了文本的上半部分.

But it is also not working and it cut off the upper half of text.

推荐答案

如果您使用的是iOS 8以上版本,请尝试此操作.它对我有用.

Try this if you are working on above iOS 8. It works for me.

cell.messageLabel.text = [_messages objectAtIndex:indexPath.row];
cell.messageLabel.lineBreakMode = NSLineBreakByTruncatingTail;
cell.messageLabel.numberOfLines=0;
return cell;

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

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

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