iOS tableViewCell添加textView设置注意事项

参考地址:http://www.cocoachina.com/ios/20141226/10778.html

在 TextViewCell.m 中实现 - (void)textViewDidChange:(UITextView *)textView,每次 text view 内容改变的时候,就重新计算一次 text view 的大小,并让 table view 更新高度。

#import "TextViewCell.h"

@implementation TextViewCell

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

  [tableView beginUpdates];

  [tableView endUpdates];

}

- (UITableView *)tableView

{

  UIView *tableView = self.superview;

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

    tableView = tableView.superview;

  }

  return(UITableView *)tableView;

}

@end


// 设置textView显示的行数

cell.contentTV.textContainer.maximumNumberOfLines = 2;        

// 设置textView超过屏幕显示省略号

cell.contentTV.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容