Cell中使用输入框
@interface TextInputCell : UITableViewCell
@end
#import "TextInputCell.h"
#import "Masonry.h"
@interface TextInputCell ()<UITextViewDelegate>
@property(nonatomic,strong)UITextView *textView;
@end
@implementation TextInputCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_textView = [[UITextView alloc] init];
_textView.scrollEnabled = NO;
_textView.delegate = self;
[self.contentView addSubview:_textView];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[_textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.offset(10.0f);
make.right.bottom.offset(-10.0f);
}];
}
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
CGRect bounds = textView.bounds;
// 计算TextView的高度
CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX);
CGSize newSize = [textView sizeThatFits:maxSize];
bounds.size = newSize;
textView.bounds = bounds;
NSLog(@"textView - %f",textView.bounds.size.height);
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
-(UITableView *)tableView
{
UIView *tableView = self.superview;
while (![tableView isKindOfClass:[UITableView class]] && tableView) {
tableView = tableView.superview;
}
return (UITableView *)tableView;
}
-(UITableView *)tableView
{
if (!_tableView) {
CGFloat x = 0.0f;
CGFloat y = 64.0f;
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.estimatedRowHeight = 10.0f;
_tableView.rowHeight = UITableViewAutomaticDimension;
[self.view addSubview:_tableView];
}
return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identifier = @"Identifier";
TextInputCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (!cell) {
cell = [[TextInputCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
}
return cell;
}
文字自适应
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_contentLabel = [[UILabel alloc] init];
_contentLabel.numberOfLines = 0;
[self.contentView addSubview:_contentLabel];
[_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.offset(10.0f);
make.bottom.right.offset(-10.0f);
}];
}
return self;
}