【问题标题】:How can i resize UITableViewCell for how much text that i have? [duplicate]我如何调整 UITableViewCell 的大小以获得我有多少文本? [复制]
【发布时间】:2012-04-05 12:27:18
【问题描述】:

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

【问题讨论】:

标签: iphone cocoa-touch ipad uitableview


【解决方案1】:

您可以在 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
}

【讨论】:

  • UILabel 可以是多行...见@Gypsa的回答。
  • 哦。很高兴知道,用 UILabel 替换我的答案中的 UITextView。但是请参阅我对@Gypsa 的评论。
【解决方案2】:

你需要实现这个:-

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

【讨论】:

  • 我认为您不想将标签高度作为表格单元格的高度来回答。我认为如果发生自动换行,您希望将高度(在格式化单行之后)增加附加行的高度。
  • 如果你只返回标签高度,你会得到一个非常小的表格行,标签上方/下方没有填充。对于类似的情况,我已经设置了自己的用于在上方/下方填充的常量(基于标准标签与一行的外观),然后在从heightForRowAtIndexPath 返回之前将它们添加到标签高度。
  • 你可以简单地做 return (CGFloat)labelsize.height+30;您可以添加一个数字使表格单元格高度大于标签高度,由您决定添加的数量。
  • 在 heightForRowAtIndexPath 中创建、配置和发布 UILabel 的意义何在?真正的测量来自对 [NSString sizeWithFont:constrainedToSize:lineBreakMode] 的调用。而且,在 cellForRowAtIndexPath 中分配自己的 UILabel 有什么意义?无论如何,您都可以将 textLabel 和 detailTextLabel 配置为随意换行。
猜你喜欢
  • 2012-11-13
  • 2015-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-18
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
相关资源
最近更新 更多