Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView in UITableView, Smooth Expand using AutoLayout

I have an editable UITextView in every UITableViewCell.

The UITextViews resize fine to pre-loaded text (calculated via heightForRowAtIndexPath) but I'm not sure how to get the smooth-resizing using AutoLayout when adding text dynamically. It just seems to get cut off.

Using AutoLayout, how can I smooth-resize a UITableViewCell and UITextView to fit the text being entered into the UITextView?

like image 761
aroooo Avatar asked Dec 26 '22 02:12

aroooo


1 Answers

Accepted answer is the deprecated one, there is a clean solution for this in iOS 8 sdk

In viewDidLoad, tell the tableView to automatically calculate row heights:

tableView.estimatedRowHeight = 44.0f;
tableView.rowHeight = UITableViewAutomaticDimension;

Implement the textViewDidChange: method of the UITextViewDelegate protocol, and tell the tableView to repaint itself every time the text is edited:

- (void)textViewDidChange:(UITextView *)textView {
[tableView beginUpdates];
[tableView endUpdates];
}

And don’t forget to set the UITextView delegate somewhere, either in Storyboard/IB or in tableView:cellForRowAtIndexPath:

like image 152
ZaEeM ZaFaR Avatar answered Jan 07 '23 23:01

ZaEeM ZaFaR