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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yun_小胖次

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

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

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

打赏作者

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

抵扣说明:

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

余额充值