dynamic调整tableView单元格的高度

我能够显示表格中的单元格内容,也Wordwrap它完全显示消息。 但是我注意到,如果内容超过了高度,那么就会变得混乱。 如果内容太大,如何增加dynamic调整单元格。 由于我从url中检索数据的长度不同,所以我不想硬编码的高度,但要改变它的内容长度。

我迄今为止所尝试的代码如下:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *person = [myPeople objectAtIndex:[indexPath row]]; NSString *personName = [person valueForKey:@"text"]; NSString *cellText = personName; UIFont *cellFont = [UIFont fontWithName:@"Helvetica-neuve" size:21.0]; CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; int buffer = 70; return labelSize.height + buffer; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [commentView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; cell.textLabel.numberOfLines = 6; cell.textLabel.font = [UIFont fontWithName:@"Helvetica-neuve" size:21.0]; [cell.textLabel setMinimumFontSize:13.0]; [cell.textLabel setAdjustsFontSizeToFitWidth:NO]; } NSDictionary *person = [myPeople objectAtIndex:[indexPath row]]; NSString *personName = [person valueForKey:@"text"]; cell.textLabel.text = personName; return cell; } 

就像我不喜欢张贴链接。 这将告诉你如何为你的细胞在飞行中计算正确的高度。

根据文字调整UILabel的高度