[要求:每当我输入UITableViewHeaderFooterView的UITextView并单击发送按钮时,我都想向UITableView添加一行。 UITableViewHeaderFooterView必须是可重用的类。
我不知道在哪里添加UITextViewDelegate方法(无论是在自定义类本身还是在使用它的主类中。)
我按照this教程创建了自定义页脚类。
textViewDidChange的逻辑
func textViewDidChange(_ textView: UITextView) {
let size = CGSize(width: view.frame.width - (sendButtonWidthConstraint.constant + textViewLeadingConstraint.constant), height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
if estimatedSize.height >= self.textViewMaxHeight
{
textView.isScrollEnabled = true
}
else {
textViewHeightConstraint.constant = estimatedSize.height
containerViewHeightConstraint.constant = textViewHeightConstraint.constant + textViewBottomConstraint.constant + textViewTopConstraint.constant
sendButtonBottomConstraint.constant = (containerViewHeightConstraint.constant - sendButtonHeightConstraint.constant)/2
textView.isScrollEnabled = false
}
}
PS:基本上,我正在尝试开发Instagram的评论功能。
您可以创建UITableViewCell
作为页眉或页脚视图以供重用。在其中实现TextViewDelegate
,然后发送回您的ViewController
。