简体   繁体   English

如何将UITextView添加到此UITableViewCell?

[英]How do I add a UITextView to this UITableViewCell?

I have a UITableViewCell that is dynamically sized based on the content in it. 我有一个UITableViewCell ,它根据其中的内容动态调整大小。 I do this in heightForRowAtIndexPath as follows: 我在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;
  }
}

In cellForRowAtIndexPath I customise the cell as follows: 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;
}

This all works fine, however I want the content of the cell to detect phone numbers, dates/times etc. So I think I need to have the content in a UITextView within the UITableViewCell to make this possible. 一切正常,但是我希望单元格的内容能够检测电话号码,日期/时间等。因此,我认为我需要将内容包含在UITableViewCellUITextView中,以实现此目的。

How can I dynamically size the UITextView and add it to the cell with the content by modifying my code above? 如何通过修改上面的代码动态调整UITextView大小并将其添加到具有内容的单元格中? Sample code please with answers because I've already tried adding the UITextView as a subview of the cells contentView but it's not working as I'd expect. 请提供示例代码并给出答案,因为我已经尝试过将UITextView添加为单元格contentView的子视图,但是它没有按我期望的那样工作。

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

While creating cell, you need to add textView as its subView and no need to set textView size separately. 创建单元格时,您需要添加textView作为其子视图,而无需单独设置textView的大小。 You just need add margin values while returning height for row in 您只需要在返回行的高度时添加边距值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在可调整大小的UITableviewCell中添加UITextView - How to add an UITextView in a resizable UITableviewCell 如何将UITextField或UITextView添加到UItableViewCell并编辑内容? - How can I add a UITextField or a UITextView to a UItableViewCell and edit the content? 当UITextView大小更改时,如何调整UITableViewCell的大小? - How do I make a UITableViewCell resize when a UITextView size changes? 如何根据其中的UITextView的大小调整UITableViewCell的大小? - How do I resize a UITableViewCell according to the size of a UITextView inside it? 如何根据UITextView子视图的大小调整UITableViewCell的大小? - How do I resize a UITableViewCell according to the size of a UITextView subView? 如何在UITableViewCell中的UIStackView中添加UIView? - How do I add a UIView to a UIStackView in a UITableViewCell? UITableViewCell-如何计算heightForRowAtIndex之前如何向UITextView添加约束 - UITableViewCell - How to Add Constraint to UITextView BEFORE heightForRowAtIndex is calculated 如何在文本后将UIImageView添加到UITextView - How do i add UIImageView to UITextView After a text Swift:如何在UIAlertController中为UITextView添加名称? - Swift: How do I add a name to the UITextView in a UIAlertController? Swift:如何在UITableViewCell firstresponder中制作UITextView - Swift: How can I make UITextView in UITableViewCell firstresponder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM