简体   繁体   中英

Dynamic height of cell with UILabel in tableview ios

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.

Here is the snapshot :- 图片

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

EDIT

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.

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

http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

This is probably one of the best tutorials on the subject that I have come across. It explains everything step by step so you can adapt things to fit your need.

Try this:

- (float)getMessageSize:(NSString *)text Width:(float)textWidth
{
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
        attributes:@{
            NSFontAttributeName: [UIFont fontWithName:fontNameRefrig size:19.0]
        }];

    CGRect rect = [attributedText boundingRectWithSize:(CGSize){textWidth, CGFLOAT_MAX}
        options:NSStringDrawingUsesLineFragmentOrigin
        context:nil];
    CGSize finalSize = rect.size;
    return finalSize.height;
}

try this code

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

return [customcell getCellHeightForText:@"text"];
}


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

 cell = [ tableView dequeueReusableCellWithIdentifier : @"customCell" ] ;
  CGRect  rect=cell.frame;
  rect.size.height = [customcell getCellHeightForText:@"text"];
 cell.frame=rect;
return cell;
}

on customcell.m file

 +(NSInteger) getCellHeightForText:(NSString *) text {
float totalHeight = 0;
float topMargin = 35;
float bottomMargin = 30;
CGSize textSize = [text calculateSize:12.0 maxWidth:195 maxHeight:0 lineBreakMode:NSLineBreakByWordWrapping];
totalHeight = totalHeight + textSize.height + topMargin + bottomMargin;
return totalHeight;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM