如何在iPhone中的detailTextLabel中增加相对于文本大小的单元格大小。

时间:2012-01-30 11:08:08

标签: iphone objective-c xcode uitableview

  • 在我的应用程序中,我有一个带有单元格样式值2的表视图:(单元格并排有两个标签)。
  • textLabel 中,我显示时间:显示在 timeLable 中。
  • detailTextLable 中,我显示了在 commentsTextView
  • 中输入的评论

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
    cell.detailTextLabel.numberOfLines = 0;
    cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;

}

cell.textLabel.text = timelabel.text;            
 cell.detailTextLabel.text = commentTextView.text;
    }
   //int x = cell.commentTextView.frame.size.height;


}

cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
cell.textLabel.textAlignment = UITextAlignmentLeft;
return cell;

}


  • 我需要根据在文本视图中输入的注释长度来扩展单元格大小:详细信息显示在textTextLabel中的'commentTextView。

  • 但是textLable(时间文本)的差异,它不会随着detailTextLabel

  • 一起扩展

该怎么办?

2 个答案:

答案 0 :(得分:1)

这一点总是很难做到,因为你需要实现这样的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *myLabel = "Hello World";
    CGSize labelSize = [[myLabel sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(160,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height;
}

答案 1 :(得分:1)

您可以使用tableview的- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath委托方法来执行此操作,例如: -

- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   CGSize lblSize ;
   NSString *lblString ;

   lblString = [[[dataSourceArray objectAtIndex:indexPath.row] valueForKey:@"aKey"];

   lblSize = [self getLebalSize:CGSizeMake(180, 9999) aLebalText:lblString aFont:[UIFont systemFontOfSize:15]];

   return lblSize.height + 10 ;
}

//getting labelsize
- (CGSize) getLebalSize : (CGSize) maxSize aLebalText : (NSString *) lebalText aFont : (UIFont *) font
{
    CGSize expectedLabelSize = [lebalText sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeTailTruncation];
    return expectedLabelSize;
}