前一阵子,遇到了这样的需求,在网上花了一番力气搜索、总结,总算是完成了这个任务,现在系统性的总结一下,分享给大家,跟着流程走,我不保证你能理解整个过程,但可以保证你能实现这个需求。本文主要以代码为主,需要解释的东西我会以注释的形式展示。复制代码
1.使用AutoLayout为自定义cell添加约束
设置cell的过程需要注意一下几个点:
- height的约束必须是大于等于某个值
- UITextView的Scrolling Enabled属性要设置为NO
- UITextView的delegate为TextViewTVCell
2.自定义cell文件内容:
//.h文件内容
#import <UIKit/UIKit.h>
@class TextViewTVCell;
@protocol TextViewTVCellDelegate <NSObject>
- (void)textViewTVCell:(TextViewTVCell *)cell didChangeText:(NSString *)text;
@end
@interface TextViewTVCell : UITableViewCell<UITextViewDelegate>
@property(nonatomic,assign) id<TextViewTVCellDelegate> delegate;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UITextView *contentTextView;
@end
//.m文件内容
#import "TextViewTVCell.h"
@implementation TextViewTVCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.contentTextView.contentInset = UIEdgeInsetsMake(-5, 0, 5, 0);
}
#pragma mark -UITextViewDelegate
-(void)textViewDidChange:(UITextView *)textView
{
if (self.delegate && [self.delegate respondsToSelector:@selector(textViewTVCell:didChangeText:)]) {
[self.delegate textViewTVCell:self didChangeText:textView.text];
}
CGRect bounds = textView.bounds;
CGSize newSize = [textView sizeThatFits:CGSizeMake(bounds.size.width, CGFLOAT_MAX)];
bounds.size = newSize;
textView.bounds = bounds;
UITableView *tableView = [self tableView];
[tableView beginUpdates];
[tableView endUpdates];
}
-(UITableView *)tableView
{
UIView *tableView = self.superview;
while (tableView && ![tableView isKindOfClass:[UITableView class]]) {
tableView = tableView.superview;
}
return (UITableView *)tableView;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
@end
复制代码
3.ViewController文件内容
//.h文件无更改,.m文件内容如下
#import "ViewController.h"
#import "TextViewTVCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,TextViewTVCellDelegate>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,copy) NSArray *dataList;
@property(nonatomic,strong) NSMutableArray *detailDataList;
@end
@implementation ViewController
static NSString *ID = @"TextViewCell";
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self performSelector:@selector(layoutUI)];
self.dataList = @[@"hello",@"hello",@"hello",@"hello",@"hello",@"hello",@"hello",@"hello",@"hello",@"hello"];
self.detailDataList =[ @[@"运行循环",@"运行循环模式是对输入源和计时器的集合进行监视,并将通知一个运行循环观察器集合",@"运行循环",@"运行循环",@"运行循环",@"运行循环",@"运行循环",@"运行循环",@"运行循环",@"运行循环"] mutableCopy];
[self.view addSubview:self.tableView];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [UIView new];
_tableView.estimatedRowHeight = 80;//先估计一个高度
_tableView.rowHeight = UITableViewAutomaticDimension;// 自适应单元格高度
[_tableView registerNib:[UINib nibWithNibName:@"TextViewTVCell" bundle:nil] forCellReuseIdentifier:ID];
}
return _tableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TextViewTVCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[TextViewTVCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
cell.delegate = self;
}
cell.nameLabel.text = self.dataList[indexPath.row];
cell.contentTextView.text = self.detailDataList[indexPath.row];
return cell;
}
#pragma mark -TextViewTVCellDelegate
-(void)textViewTVCell:(TextViewTVCell *)cell didChangeText:(NSString *)text
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
self.detailDataList[indexPath.row] = text;
}
@end复制代码