【问题标题】:How to add UITextView to custom UITableViewHeaderFooterView?如何将 UITextView 添加到自定义 UITableViewHeaderFooterView?
【发布时间】:2020-05-24 14:41:16
【问题描述】:

要求:每当我输入 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 的 cmets 功能。

【问题讨论】:

  • 您可以将其中任何一个设置为 UITextViewDelegate。设置一个易于管理的位置。
  • 我认为您需要将 ViewController 设置为 UITextView 委托。

标签: ios uitableview uitextview swift5


【解决方案1】:

您可以创建UITableViewCell 作为页眉或页脚视图以供重复使用。在其中实现TextViewDelegate,然后发送回您的ViewController

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多