Last active
June 16, 2020 00:16
-
-
Save DDavis1025/55cef296f343cdeece027c4e14605d5e to your computer and use it in GitHub Desktop.
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 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) | |
} | |
} | |
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 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