Skip to content

Instantly share code, notes, and snippets.

@hilalbaig
Last active March 1, 2019 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilalbaig/ed7647f494db97296360f0882e64870b to your computer and use it in GitHub Desktop.
Save hilalbaig/ed7647f494db97296360f0882e64870b to your computer and use it in GitHub Desktop.
Easy way to automatically manage height for growing UITextView embedded in UITableViewCell instead of manually calculating and providing it for each cell.
protocol CommentCellDelegate: class {
func updateHeight()
}
class CommentCell: UITableViewCell, UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
delegate?.updateHeight()
}
}
class TestViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CommentCell = tableView.dequeueReusableCell...
cell.delegate = self
return cell
}
}
extension TestViewController: CommentCellDelegate {
func updateHeight() {
UIView.setAnimationsEnabled(false)
tableView.beginUpdates()
tableView.endUpdates()
UIView.setAnimationsEnabled(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment