【问题标题】:How do I add a UITextView to this UITableViewCell?如何将 UITextView 添加到此 UITableViewCell?
【发布时间】:2013-02-20 16:50:41
【问题描述】:

我有一个UITableViewCell,它根据其中的内容动态调整大小。我在heightForRowAtIndexPath 中这样做如下:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.section == 1 && indexPath.row == 1) {
    NSDictionary *fields = self.messageDetailsDictionary[@"fields"];
    NSString *cellText = fields[@"message_detail"];
    UIFont *cellFont = [UIFont systemFontOfSize:14.0];
    CGSize constraintSize = CGSizeMake(self.tableView.frame.size.width - 50.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    return labelSize.height + 20.0f;
  } else {
    return tableView.rowHeight;
  }
}

cellForRowAtIndexPath我自定义单元格如下:

case 1:{
  UITableViewCell *cell = [[UITableViewCell new] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailCell"];
  if (cell == nil) {
    cell = [[UITableViewCell new] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailCell"];
  }
  NSDictionary *fields = self.messageDetailsDictionary[@"fields"];
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
  cell.textLabel.font = [UIFont systemFontOfSize:14.0];
  cell.textLabel.numberOfLines = 0; // This means multiline
  cell.textLabel.text = fields[@"message_detail"];
  return cell;
}

这一切都很好,但是我希望单元格的内容能够检测电话号码、日期/时间等。所以我认为我需要在UITableViewCell 中的UITextView 中包含内容才能实现这一点。

如何通过修改上面的代码来动态调整UITextView 的大小并将其添加到包含内容的单元格中?示例代码请提供答案,因为我已经尝试将 UITextView 添加为单元格 contentView 的子视图,但它没有像我预期的那样工作。

【问题讨论】:

    标签: ios uitableview uitextview


    【解决方案1】:

    您可以使用sizeToFitsizeThatFits: 方法来调整UITextView 对象的大小

    【讨论】:

      【解决方案2】:

      在创建单元格时,您需要添加 textView 作为其子视图,无需单独设置 textView 大小。您只需要在返回行的高度时添加边距值

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

      【讨论】:

        猜你喜欢
        • 2021-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多