8

I have a UITextView in a tableViewCell, and using the iOS 8 provided UITableViewAutomaticDimension approach listed here.

If user add new line to UITextView, and so textView, cell height should increase, how can I update height?

Tried:

cell.layoutIfNeeded()
cell.setNeedsUpdateConstraints()

These does not work.

If I reload cell, like

tableView.reloadRowsAtIndexPaths([iindexPath], withRowAnimation: .None)

this works, but then I need to deal with resign and show keyboard. I would avoid it. Any workaround?

Community
  • 1
  • 1
János
  • 32,867
  • 38
  • 193
  • 353

2 Answers2

8

need to add following method:

func textViewDidChange(textView: UITextView) {

    tableView.beginUpdates()
    tableView.endUpdates()
}

it will update constraints without reloading tableview

János
  • 32,867
  • 38
  • 193
  • 353
0

After sometime textView will start scrolling to make increase it height on new line ever time you need to implement textViewDidChangeText: method and increase its height by calculating height for text.

To use UITableViewAutomaticDimension you need to add constraints to subviews from top to bottom, I think you should also create and IBOutlet for height constraint of textView and set constant value on textViewDidChangeText method, in that case tableViewCell height will change dynamically.

Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54