UITableView上放 textViw输入时自动计算高度

直接上代码

HHArticleTextView *textView = [[HHArticleTextView alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-20, 34)];
    self.textView = textView;
    textView.textColor = [UIColor blackColor];
    textView.font = [UIFont systemFontOfSize:TextFontSize];
    textView.textViewPlacehold = @"呵呵";
    textView.scrollEnabled = NO;
    textView.showsVerticalScrollIndicator = NO;
    textView.showsHorizontalScrollIndicator = NO;
    textView.backgroundColor = [UIColor clearColor];
    textView.delegate = self;
    [self.contentView addSubview:textView];
    if (@available(iOS 11.0, *)) {
        textView.textDragInteraction.enabled = NO;
    }

注:HHArticleTextView是我自定义的一个 textView 可以直接用系统的 UITextView

HHArticleEditingTextTableViewCell.h

@interface HHArticleEditingTextTableViewCell : UITableViewCell<UITextViewDelegate>
/**
 *
 */
@property (nonatomic,strong) HHArticleTextView *textView;
/**
 *
 */
@property (nonatomic,strong) HHArticleEditingModel *model;
/**
 *
 */
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic, readonly) CGFloat cellHeight;
@end
HHArticleEditingTextTableViewCell.m

#import "HHArticleEditingTextTableViewCell.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define kPadding 5

@implementation HHArticleEditingTextTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createUI];
    }
    return self;
}
- (void)createUI{
    HHArticleTextView *textView = [[HHArticleTextView alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-20, 34)];
    self.textView = textView;
    textView.textColor = [UIColor blackColor];
    textView.font = [UIFont systemFontOfSize:TextFontSize];
    textView.textViewPlacehold = @"呵呵";
    textView.scrollEnabled = NO;
    textView.showsVerticalScrollIndicator = NO;
    textView.showsHorizontalScrollIndicator = NO;
    textView.backgroundColor = [UIColor clearColor];
    textView.delegate = self;
    [self.contentView addSubview:textView];
    if (@available(iOS 11.0, *)) {
        textView.textDragInteraction.enabled = NO;
    }
}

- (CGFloat)cellHeight
{
    return [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, FLT_MAX)].height + kPadding * 2;
}
- (void)setModel:(HHArticleEditingModel *)model{
    _model = model;
    self.textView.text = _model.text;
}

- (void)textViewDidChange:(UITextView *)TextView
{
//    CGFloat newHeight = [self cellHeight];

    self.textView.placeholdLabel.hidden = TextView.text.length>0;
    
    _model.text = TextView.text;
    CGRect frame = self.textView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [self.textView sizeThatFits:constraintSize];
    self.textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
    [_tableView beginUpdates];
    [_tableView endUpdates];
}

@end

tableView创建的时候要加上下面三句不然 输入的时候 tableView 跳动的很厉害

//防止 tableView 输入时跳动
    tableView.estimatedRowHeight = 0;
    tableView.estimatedSectionHeaderHeight = 0;
    tableView.estimatedSectionFooterHeight = 0;

在这个方法里面要把 tableView 传到Cell 里面(cell.tableView = self.tableView;)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *identifierID = @"TextTableViewCell";
        HHArticleEditingTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
        if (!cell) {
            cell = [[HHArticleEditingTextTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.tableView = self.tableView;
        cell.model = model;
        return cell;
}

在设置高的时候把获取到cell 的cellHeight属性直接赋值

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HHArticleEditingTextTableViewCell * cell = (HHArticleEditingTextTableViewCell *)[tableView.dataSource tableView:_tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight<44 ? 44 : cell.cellHeight;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 昨天看了77关于理财的分享,很是触动。佩服她学习简七理财后消化吸收并高效产出的能力,由这一点,又领略了她真正学霸的...
    绽蕊向阳阅读 147评论 0 0
  • 你可曾体会过不知前路该往哪走的犹豫,你会害怕吗?其实无论走哪一条路,并没有好好坏坏,只是结果不同。用手机写文章的感...
    小小Sera阅读 893评论 0 3
  • 小学到初中英语成绩一直不错,高中因不喜欢英语老师而失去认真学习英语的机会。重拾英语的想法一直都在内心荡起。正好今日...
    杨杨Jenny阅读 171评论 7 3
  • 第一种类型的分页 条件如下 最多显示5页(可动态调整) 后台返回总页数 pages 后台返回当前页 curre...
    天堂宝宝_V阅读 1,139评论 0 3