NS_ASSUME_NONNULL_BEGIN
@interface AutoCell : UITableViewCell
@property (nonatomic, copy) void(^callbackTextViewChangeText)(NSString *text)
@end
NS_ASSUME_NONNULL_END
///==========================================
@interface AutoCell()<UITextViewDelegate>
@property (nonatomic, strong) UITextView *cusTextView
@end
@implementation AutoCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.selectionStyle = UITableViewCellSelectionStyleNone
self.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.1]
self.contentView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.1]
[self.contentView addSubview:self.cusTextView]
[self p_addMasonry]
}
return self
}
- (void)p_addMasonry {
[self.cusTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0);
make.left.mas_equalTo(15);
make.right.mas_equalTo(-15);
make.height.mas_equalTo(80).priorityHigh();
make.bottom.mas_equalTo(0).priorityLow();
}]
}
- (UITextView *)cusTextView {
if (!_cusTextView) {
_cusTextView = [[UITextView alloc] init]
_cusTextView = [[UITextView alloc] init]
_cusTextView.delegate = self
_cusTextView.textColor = [UIColor whiteColor]
_cusTextView.font = [UIFont systemFontOfSize:30]
_cusTextView.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.1]
}
return _cusTextView
}
- (void)textViewDidChange:(UITextView *)textView {
if (self.callbackTextViewChangeText) {
self.callbackTextViewChangeText(textView.text)
}
CGRect rect = textView.bounds
UITableView *tableView = [self tableView]
CGSize maxSize = CGSizeMake(rect.size.width, CGFLOAT_MAX)
CGSize newSize = [textView sizeThatFits:maxSize]
if (newSize.height > 80) {
[_cusTextView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(newSize.height);
}]
[tableView beginUpdates]
[tableView endUpdates]
}
}
- (UITableView *)tableView {
UIView *tableView = self.superview
while (![tableView isKindOfClass:[UITableView class]] && tableView) {
tableView = tableView.superview
}
return (UITableView *)tableView
}
@end
@interface ViewController : UIViewController
@end
///==========================================
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
self.title = @"cell自适应textView高度"
[self.view addSubview:self.tableView]
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AutoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AutoCell class]) forIndexPath:indexPath]
cell.callbackTextViewChangeText = ^(NSString *text) {
NSLog(@"%@",text)
}
return cell
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]
_tableView.delegate = self
_tableView.dataSource = self
_tableView.estimatedRowHeight = 80
_tableView.rowHeight = UITableViewAutomaticDimension
_tableView.sectionHeaderHeight = 0.0
_tableView.sectionFooterHeight = 0.0
_tableView.estimatedSectionHeaderHeight = 0.0
_tableView.estimatedSectionFooterHeight = 0.0
_tableView.showsVerticalScrollIndicator = NO
_tableView.showsHorizontalScrollIndicator = NO
[_tableView registerClass:[AutoCell class] forCellReuseIdentifier:NSStringFromClass([AutoCell class])]
}
return _tableView
}
@end
