UITextView in UITableView, Smooth Expand with AutoLayout

I have an editable UITextView in every UITableViewCell .

UITextViews resize to pre-loaded text (calculated via heightForRowAtIndexPath ), but I'm not sure how to get smooth resizing using AutoLayout when adding text dynamically. It seems to be clipped.

Using AutoLayout , how can I flatten the size of a UITableViewCell and UITextView to insert the text that was entered into the UITextView ?

+4
source share
2 answers

You can not. A table cell does not display its height using layout constraints, but through the delegate method tableView:heightForRowAtIndexPath:

You can probably achieve the effect that you perform as follows:

  • Attach text to the top and bottom of the cell content view.
  • In the text view delegate methods, recalculate the size of the text as you type.
  • If resizing is required, update wherever you are in your model, heights are saved
  • Call beginUpdates and endUpdates as a table. This will make it request heights and animate the new size (by extension, also resizing the text view).
+3
source

The accepted answer is outdated, for this purpose pure iOS 8 sdk

In viewDidLoad, tell the View table to automatically calculate row heights:

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

Implement the textViewDidChange: UITextViewDelegate method and tell the tableView to redraw every time the text is edited:

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

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

+7
source

Source: https://habr.com/ru/post/1483971/


All Articles