Skip to content

Instantly share code, notes, and snippets.

@DDavis1025
Last active June 16, 2020 00:16
Show Gist options
  • Save DDavis1025/55cef296f343cdeece027c4e14605d5e to your computer and use it in GitHub Desktop.
Save DDavis1025/55cef296f343cdeece027c4e14605d5e to your computer and use it in GitHub Desktop.
protocol HeightForTextView {
func heightOfTextView(height: CGFloat)
}
class CommentCell:UITableViewCell {
var delegate:HeightForTextView?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
textViewContstraints()
}
func textViewDidChange(textView: UITextView) {
var fixedWidth: CGFloat = textView.frame.size.width
var newSize: CGSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
if let iuDelegate = self.delegate {
print("delegate text view")
iuDelegate.heightOfTextView(height: newSize.height)
}
}
func textViewContstraints() {
textView.translatesAutoresizingMaskIntoConstraints = false
var frame = self.textView.frame
frame.size.height = self.textView.contentSize.height
self.textView.frame = frame
textViewDidChange(textView: self.textView)
}
}
class CommentVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, HeightForTextView {
func heightOfTextView(height: CGFloat) {
textViewHeight = height
self.myTableView.beginUpdates()
self.myTableView.endUpdates()
}
var textViewHeight = CGFloat()
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return textViewHeight + 70
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) as! CommentCell
cell.delegate = self
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment