https://github.com/zhitrend/autocelltextview
UITableview中添加UITextView,不去处理textView文字让tableview高度自动添加
https://github.com/zhitrend/autocelltextview
Last synced: 11 months ago
JSON representation
UITableview中添加UITextView,不去处理textView文字让tableview高度自动添加
- Host: GitHub
- URL: https://github.com/zhitrend/autocelltextview
- Owner: zhitrend
- Created: 2018-06-14T02:19:02.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-14T02:27:25.000Z (about 8 years ago)
- Last Synced: 2025-03-24T09:17:40.334Z (about 1 year ago)
- Language: Objective-C
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AutoCellTextView
面试题:UITableview中添加UITextView,不去处理textView文字让tableview高度自动添加
原理:
1、使用UITableView+FDTemplateLayoutCell和Masonry结合自动布局扩展高度
2、通过cell中的textView的内容变化代理让tableView重新处理高度
主要代码实现
```
// cell中textView内容代理到控制器
- (void)cellTextViewDidChange:(UITextView *)textView {
CGSize maxSize = CGSizeMake(textView.bounds.size.width, CGFLOAT_MAX);
CGSize newSize = [textView sizeThatFits:maxSize];
CGRect bounds = textView.bounds;
bounds.size = newSize;
textView.bounds = bounds;
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
```
```
// tableViewCell高度处理
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
AutoTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect rect = cell.textview.bounds;
if (rect.size.height != 0) {
return rect.size.height;
}
return [tableView fd_heightForCellWithIdentifier:identifier cacheByIndexPath:indexPath configuration:^(AutoTableViewCell *cell) {
cell.data = @{};
}];
}
```
```
// FDTemplateLayoutCell和Masonry结合自动布局扩展高度
[self.textview mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView.mas_top);
make.left.equalTo(self.contentView.mas_left);
make.right.equalTo(self.contentView.mas_right);
make.height.lessThanOrEqualTo(@300);
make.height.greaterThanOrEqualTo(@150);
make.bottom.equalTo(self.contentView.mas_bottom);
}];
```