这篇文章是为了解决在tableView上加textview,但是cell高度是随着输入实时变化的,以及textview换行是上下抖动问题
话不多说,直接上代码
if (@available(iOS 11.0, *)) {
_mytable.estimatedRowHeight = 0;
_mytable.estimatedSectionFooterHeight = 0;
_mytable.estimatedSectionHeaderHeight = 0;
}
[_mytable beginUpdates];[_mytable endUpdates];这两个方法可以只刷新cell高度, reloaddata会时输入失去焦点,不赞成使用
- (void)textViewDidChange:(UITextView *)textView {
_dataArray[textView.tag-100] = textView.text;
[textView sizeToFit];//使用sizeToFit方法处理textview随输入大小变化以及自动换行问题
_height=textView.frame.size.height; //_height记录textview高度
textView.frame = CGRectMake(0, 0, 300, textView.frame.size.height);
[_mytable beginUpdates];
[_mytable endUpdates];
}
将textview加在cell上,
UITextView * textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 300, 100) textContainer:nil];
textView.backgroundColor = [UIColor greenColor];
textView.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
textView.textColor = [UIColor colorWithRed:74/255 green:74/255 blue:74/255 alpha:1];
textView.tag=indexPath.row+100;
textView.text=_dataArray[indexPath.row];
[cell.contentView addSubview:textView];
textView.delegate = self;
// 以下两行代码是为了换行不抖动
textView.layoutManager.allowsNonContiguousLayout=NO;
textView.scrollEnabled = NO;
[textView sizeToFit];//使用sizeToFit方法处理textview随输入大小变化以及自动换行问题
textView.frame = CGRectMake(0, 0, 300, textView.frame.size.height);
_height=textView.frame.size.height; //_height记录textview高度
我这个是只有一个cell有输入框,所以简单处理了,如果有多行的,也可以加以区分处理。是不是很简单啊