如何利用autolayout动态计算UITableViewCell的高度
2015-03-18 11:53
411 查看
公司最近要用到autoLayout,今天看了一些autoLayout相关的东西。临下班的时候,一个同事问到如何使用autoLayout实现动态计算UITableViewCell高度,于是一起研究了一番,参考了一篇动态计算UITableViewCell高度详解文章,回家简单实现了使用autoLayout实现了动态计算UITableViewCell高度。
实现上面的步骤其实很简单:
1.在Cell对应的xib文件中建立完整的约束。
2.使用
然后在方法
注意:
相关代码:
实现上面的步骤其实很简单:
1.在Cell对应的xib文件中建立完整的约束。
2.使用
[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]方法对cell进行动态计算高度,
然后在方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath返回cell的高度。
注意:
[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]一定是cell.contentView,不能用cell。
相关代码:
- (void)viewDidLoad{ [super viewDidLoad]; _data = @[@"数据1", @"数据2",@"数据3", @"数据4", @"数据5", @"数据6"]; _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; _cell = [[[NSBundle mainBundle] loadNibNamed:@"LWCell" owner:self options:nil] lastObject]; _textViewCell = [[[NSBundle mainBundle] loadNibNamed:@"LWTextViewCell" owner:self options:nil] lastObject]; _textViewCell.textView.delegate = self; }
#pragma mark UITableView delegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _data.count+5; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row < _data.count){ LWCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PrCell"]; if(cell == Nil){ cell = [[[NSBundle mainBundle] loadNibNamed:@"LWCell" owner:self options:nil] lastObject]; } cell.content = _data[indexPath.row]; return cell; } else { LWTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHTextViewCell"]; if(cell == nil){ cell = [[[NSBundle mainBundle] loadNibNamed:@"LWTextViewCell" owner:self options:nil] lastObject]; cell.textView.delegate = self; } return cell; } }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row < _data.count){ _cell.content = _data[indexPath.row]; CGSize size = [_cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; return size.height+1.0f; } else { CGSize size = [_textViewCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; _textViewCell.textView.text = _inputText; CGSize textViewSize = [_textViewCell.textView sizeThatFits:CGSizeMake(_textViewCell.textView.frame.size.width, 100000.0f)]; return 50 > (size.height + 1.0f + textViewSize.height) ? 50 : (size.height + 1.0f + textViewSize.height); } }
#pragma mark UITextView delegate- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"/n"]) { NSLog(@"h=%f", textView.contentSize.height); } return YES; }
- (void)textViewDidChange:(UITextView *)textView { _inputText = textView.text; [_tableView beginUpdates]; [_tableView endUpdates]; }
相关文章推荐
- 如何利用autolayout动态计算UITableViewCell的高度
- 动态计算UITableViewCell高度详解 Auto Layou + Manual Layout
- iOS: Autolayout和UITableViewCell的动态高度 (1)
- Auto Layout 使用心得(五)—— 根据文字、图片自动计算 UITableViewCell 高度
- iOS: Autolayout和UITableViewCell的动态高度 (2) – 多样式Cell切换
- iOS: Autolayout和UITableViewCell的动态高度 (1)
- iOS: Autolayout和UITableViewCell的动态高度 (1)
- iOS: Autolayout和UITableViewCell的动态高度 (2) – 多样式Cell切换
- 源码推荐(4.22):AutoLayout下自动计算UITableViewCell高度的扩展,常用加密算法
- iOS: Autolayout和Storyboard中动态UITableViewCell的高度
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解
- 使用Autolayout xib实现动态高度的TableViewCell
- AutoLayout——由切图引发的UITableViewCell自定义高度布局的失败!
- 使用Autolayout xib实现动态高度的TableViewCell
- 动态计算UITableViewCell高度详解
- Fantageek翻译系列之《使用Autolayout显示变化高度的UITableViewCell》
- 【转】动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解