Last active
March 1, 2019 05:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol CommentCellDelegate: class { | |
func updateHeight() | |
} | |
class CommentCell: UITableViewCell, UITextViewDelegate { | |
func textViewDidChange(_ textView: UITextView) { | |
delegate?.updateHeight() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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