公司最近要用到autoLayout,今天看了一些autoLayout相关的东西。临下班的时候,一个同事问到如何使用autoLayout实现动态计算UITableViewCell高度,于是一起研究了一番,参考了一篇动态计算UITableViewCell高度详解文章,回家简单实现了使用autoLayout实现了动态计算UITableViewCell高度。
实现上面的步骤其实很简单:
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 = @[@"我的境况放假", @"jkdjfkadjfdjafj的积分卡积分卡大家反馈到敬爱放大家分开阿娇快放假啊积分卡积分积分开始就发的是减肥打手机发电视剧发的是开放的是的空间按附件的垃圾费看得见啊",@"jkdjfka的境况放假大放假打卡机飞就看得见反馈的减肥", @"jkdjajfldajklf 贷记卡家乐福大家快乐防静电阿里的积分大姐夫", @"放假啊考虑放假了案件反馈的安家费看大家分开打反馈到敬爱放的骄傲看反馈卡就飞快的放假", @"jkdja将开发大姐夫就打开附件安定放假开打放假的卡风较大飞交罚款的姐夫"]; _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; _cell = [[[NSBundle mainBundle] loadNibNamed:@"PrCell" owner:self options:nil] lastObject]; _textViewCell = [[[NSBundle mainBundle] loadNibNamed:@"PrTextViewCell" owner:self options:nil] lastObject]; _textViewCell.textView.delegate = self; // Do any additional setup after loading the view, typically from a nib. } #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){ PrCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PrCell"]; if(cell == Nil){ cell = [[[NSBundle mainBundle] loadNibNamed:@"PrCell" owner:self options:nil] lastObject]; } cell.content = _data[indexPath.row]; return cell; } else{ PrTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PrTextViewCell"]; if(cell == nil){ cell = [[[NSBundle mainBundle] loadNibNamed:@"PrTextViewCell" 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]; }
参考:
http://www.cocoachina.com/industry/20140604/8668.html
http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan-uitableviewcellgao-du-xiang-jie/