IOS开发之uitextview动态适应高度讲解
1. self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 44; 2. 在heightForRowAtIndexPath中设置return UITableViewAutomaticDimension; 3. - (void)textViewDidChange:(UITextView *)textView { CGRect bounds = textView.bounds; // 计算 text view 的高度 CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX); CGSize newSize = [textView sizeThatFits:maxSize]; bounds.size = newSize; textView.bounds = bounds; // 让 table view 重新计算高度 //2.textView的高度不想等就更新 让 table view 重新计算高度 if (bounds.size.height != self.contentView.bounds.size.height) { UITableView *tableView = [self tableView]; [tableView beginUpdates]; [tableView endUpdates]; } self.contentView.bounds = bounds; } - (UITableView *)tableView { UIView *tableView = self.superview; while (![tableView isKindOfClass:[UITableView class]] && tableView) { tableView = tableView.superview; } return (UITableView *)tableView; }