我如何调整UITableViewCell的大小,我有多少文字?

时间:2012-03-21 00:24:02

标签: iphone cocoa-touch ipad uitableview

我从服务器获取了一些文本,并且我将它添加到UILabel,它将添加到UITableViewCell,并且每次它可以是微小的或大的或多行时这个文本都会改变。我的问题是如何让这个UILabel自动适应文本(多行)并调整UITableViewCell的大小以适应UILabel?

2 个答案:

答案 0 :(得分:2)

您可以在tableView的数据源中执行以下操作:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  // for the sake of illustration, say your model for this table is just an array of strings you wish to present.
  // it's probably more complex, but the idea is to get the string you want to present for the
  // cell at indexPath
  NSString *stringForThisCell = [self.model objectAtIndex:indexPath.row];

  // you can get fancier here, and dynamically get the font from the UITextView prototype
  // but for simplicity, just copy the font size you configured the text view with in your nib
  CGSize size = [stringForThisCell sizeWithFont:[UIFont systemFontOfSize:14.0]];

  // this is a little funky, because for it to be just right, you should format your prototype
  // cell height to be a good-looking height when your text view has a zero height.
  // the basic idea here is that the cell will get taller as the text does.
  return tableView.rowHeight + size.height;
}

然后,在配置单元格时,以相同的方式获取字符串和大小,并设置UITextView框架以匹配大小

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


      NSString *stringForThisCell = [self.model objectAtIndex:indexPath.row];
      CGSize size = [stringForThisCell sizeWithFont:[UIFont systemFontOfSize:14.0]];
      UITextView *myTextView = (UITextView *)[cell viewWithTag:kMY_TEXT_VIEW_TAG];  // make this tag match a tag you give the text view in the prototype cell

      myTextView.frame = CGRectMake(myTextView.frame.origin.x, myTextView.frame.origin.y, size.width, size.height);
      // the rest of your configure cell
}

答案 1 :(得分:1)

你需要实现这个: -

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

{
    CGSize labelsize;
    UILabel * textDesc1 = [[UILabel alloc] init];
    [textDesc1 setNumberOfLines:0];
    textDesc1.text=[self.blogTextArray objectAtIndex:indexPath.row];//replace with your own text
    [textDesc1 setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
    labelsize=[textDesc1.text sizeWithFont:textDesc1.font constrainedToSize:CGSizeMake(320, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    [textDesc1 release];
    return (CGFloat)labelsize.height; 


}
- (UITableViewCell *)tableView:(UITableView *)tbleView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize labelsize;
    UILabel *commentsTextLabel = [[UILabel alloc] init];;
    [commentsTextLabel setNumberOfLines:0];
    [commentsTextLabel setBackgroundColor:[UIColor clearColor]];
    NSString *text=[self.blogTextArray objectAtIndex:indexPath.row];//replace with your own text
commentsTextLabel.text=text;
    [commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
    labelsize=[text sizeWithFont:commentsTextLabel.font constrainedToSize:CGSizeMake(320, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    commentsTextLabel.frame=CGRectMake(0, 0, 320, labelsize.height);
    [cell.contentView addSubview:commentsTextLabel];
    [commentsTextLabel release];
}