iOS将UITextView嵌套在UITableViewCell中实时更新cell的高度

1、在tableviewcell拖一个UITextView,设置上下左右的约束;

2、记得再给UITextView设置一个大于等于的高度,必须大于0;(注意:因为UITextView顶部有个UILabel和UIButton,而我的UITextView顶部是依据UILabel的底部为约束,所以UILabel要添加一个固定高度的约束

3、记得把UITextView的Scrolling Enabled设置为false。就是取消勾选(这一点很关键,如果不设置为 NO,UITextView 在内容超出 frame 后,重新设置 text view 的高度会失效,并出现滚动条。

4、在控制器设置UITableView的默认高度和cell的高度使用自适应高度

//  支持自适应 cell

  self.tableView.estimatedRowHeight = 100;

  self.tableView.rowHeight = UITableViewAutomaticDimension;

5、在UITableViewCell上面,将UITextView的delegat 对象设置为cell上面;

在UITextViewDelegate下面的方法加上刷新的代码

- (void)textViewDidChange:(UITextView *)textView

{

  CGRect bounds = textView.bounds;

  // 计算 text view 的高度

  CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX);

  CGSize newSize = [textView sizeThatFits:maxSize];

  bounds.size = newSize;

  textView.bounds = bounds;

  // 让 table view 重新计算高度

  UITableView *tableView = [self tableView];

// 让 table view 的高度进行刷新,必须要调用以下两个方法

  [tableView beginUpdates];

  [tableView endUpdates];

}

- (UITableView *)tableView

{

  UIView *tableView = self.superview;

  while (![tableView isKindOfClass:[UITableView class]] && tableView) {

    tableView = tableView.superview;

  }

  return (UITableView *)tableView;

}

6、因为tableview滚动的时候的重用机制,会把之前输入的内容给情况,所以在输入的内容的时候,需要把内容给存起来。不会导致内容丢失的情况

 

7、在UITableviewCell中添加一个委托

@protocol TextViewCellDelegate;

@interface TextViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (weak, nonatomic) id<textviewcelldelegate> delegate;

@end

@protocol TextViewCellDelegate <nsobject>

- (void)textViewCell:(TextViewCell *)cell didChangeText:(NSString *)text;

@end

 

8、然后在UITextView的delegate的textViewDidChange中把内容委托出去,让外部进行存储操作

- (void)textViewDidChange:(UITextView *)textView

{

  if ([self.delegate respondsToSelector:@selector(textViewCell:didChangeText:)]) {

    [self.delegate textViewCell:self didChangeText:textView.text];

  }

  // 计算 text view 的高度

  ...

  // 让 table view 重新计算高度

  ...

}

 

9、最后在视图控制器类进行数据的存储

#pragma mark - TextViewCellDelegate

- (void)textViewCell:(TextViewCell *)cell didChangeText:(NSString *)text

{

  NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

  NSMutableArray *data = [self.data mutableCopy];

  data[indexPath.row] = text;

  self.data = [data copy];

}

 

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

  return [self.data count];

}

 

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

  TextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextViewCell" forIndexPath:indexPath];

  cell.textView.text = self.data[indexPath.row];

  return cell;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,您需要将 YYLabel 添加到 UITableViewCell 。 您可以通过在 UITableViewCell 子类创建 YYLabel 实例并将其添加到 contentView 来完成此操作。 您可以使用自动布局或手动布局将 YYLabel 放置在所需的位置上。 接下来,您需要创建 NSMutableAttributedString,其包含 UITextView 和其他文本。 然后,您可以使用 NSMutableAttributedString 的 insert 方法将 UITextView 作为附件添加到文本。 以下是一个示例代码片段: ```swift let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 50)) textView.text = "This is a UITextView" let attachment = NSTextAttachment() attachment.bounds = CGRect(x: 0, y: 0, width: 100, height: 50) attachment.setAttachmentContent(textView) let attributedString = NSMutableAttributedString(string: "This is a YYLabel with a UITextView attachment") attributedString.insert(NSAttributedString(attachment: attachment), at: 21) yyLabel.attributedText = attributedString ``` 最后,您需要处理 UITextViewUITableView 之间的交互。您需要将 UITextView 的 isEditable 属性设置为 false,以防止用户编辑文本。您还需要在 UITableViewDelegate 实现 heightForRowAt 和 estimatedHeightForRowAt 方法来动态计算 UITableViewCell高度,以适应包含 UITextView 的 YYLabel。 以下是一个示例 UITableViewDelegate 实现: ```swift class MyTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cell") } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell // Configure the cell... return cell } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MyTableViewCell cell.configure(with: "This is a YYLabel with a UITextView attachment") return cell.systemLayoutSizeFitting(CGSize(width: tableView.frame.width, height: UIView.layoutFittingCompressedSize.height)).height } override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } } ``` 请注意,此示例实现了一个名为 MyTableViewCellUITableViewCell 子类,该子类包含一个名为 yyLabel 的 YYLabel 实例。 该子类还实现了一个名为 configure(with:) 的方法,该方法接受一个字符串参数,并在 YYLabel 设置带有 UITextView 附件的 NSMutableAttributedString。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yun_小胖次

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值