【问题标题】:How to implement live word count for UITableViewCell TextView object?如何为 UITableViewCell TextView 对象实现实时字数统计?
【发布时间】:2019-01-17 01:37:59
【问题描述】:

在另一个视图中,我有这个函数(如下),它根据给定条件对 TextView 对象进行实时字数统计,然后将其显示在标签上。我想对我的 TableView Cells 做同样的事情。但是,这个函数显然不起作用,因为它需要从单元格中计算出的“savedHashtagTextView”(textView)和“hashtagCount”(标签)字段。我不确定如何完成这项工作。请帮忙 - 我已经坚持了好几个小时了!

func textViewDidChange(_ textView: UITextView) {
        let components = savedHashtagTextView.text.components(separatedBy: "#")
        hashtagCount.text = String(components.count)
        if hashtagCount.text == "0" {
            hashtagCount.text = ""
        }
}

【问题讨论】:

  • 对不起,如果我不完全理解您的方法,但是您不能将相同的数据传递给 tableview 单元格,就像您为 textview 所做的那样?也许发布一些你的单元格对象和表格视图的代码。
  • 别担心,我很难解释。我在上面编辑了我的问题并删除了不必要的信息。但是,要回答您的问题,此函数不知道它将读取哪个单元格的“savedHashtagTextView”以及它将修改哪个“hashtagCount”标签。因此,我的问题是弄清楚如何设计这个函数,以便它完成它应该做的事情,但也知道它正在修改哪个单元格。我希望这会有所帮助!
  • 好的,我明白了,您正在计算每个单元格中的单词并将该计数提供给可能位于同一视图控制器中的标签。我相信所做的回答很有帮助。

标签: swift uitableview uitextview


【解决方案1】:

您将需要使用UITextViewDelegate 协议创建一个自定义UITableViewCell 对象,以便获取特定UITableViewCell UITextView 的字数。此外,您需要以某种方式将字数发送到ViewController,以便您可以更新字数UILabel。您可以通过创建自己的 CustomCellDelegate 协议来实现此目的,该协议需要 updateWordCount() 函数,然后在您的 ViewController 中使用此协议。

当字数统计更新后,您现在需要使用CustomCellDelegate 协议调用位于您的ViewController 中的updateWordCount() 函数。

作为一个例子,这是你想要做的:

如下创建一个CustomCell.swift文件:

// Create the protocol
protocol CustomCellDelegate {
    func updateWordCount(count: Int)
}

class CustomCell: UITableViewCell, UITextViewDelegate {
    // Your UITextView
    @IBOutlet weak var textView: UITextView!
    // The cells delegate -- which we will set to self when initiated
    var delegate: CustomCellDelegate?

    func textViewDidChange(_ textView: UITextView) {
         // Get the word count here
         let wordCount = 10
         // Now we call the delegate to send the wordCount to the ViewController
         delegate?.updateWordCount(count: wordCount)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        textView.delegate = self
    }
}

现在在情节提要中创建自定义单元格,并为其使用CustomCell 类,同时将其identifier 设置为“CustomCell”。另外,请确保将您的 UITextView 链接到 CustomCell 类中的插座。

现在在包含UITableViewViewController 中:

// Use the CustomCellDelegate protocol here
class YourViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomCellDelegate {
    @IBOutlet weak var wordCountLabel: UILabel!
    // This is the function required by the CustomCellDelegate
    func updateWordCount(count: Int) {
        wordCountLabel.text = "Word Count: \(count)"
    }

    // Set up the custom Cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
        // Setup the delegate
        cell.delegate = self
        return cell
    }
}

警告:此代码未经测试,我将其全部写在 StackOverflow 上。可能存在语法错误,但除了一些潜在的语法错误之外,这将起作用。

编辑:

根据您的评论,字数标签位于单元格中。这意味着我们不再需要CustomCellDelegate 协议,并且可以进行一些小的更改来完成这项工作。

CustomCell.swift:

class CustomCell: UITableViewCell, UITextViewDelegate {
    // Your UITextView
    @IBOutlet weak var textView: UITextView!
    // Your Label
    @IBOutlet weak var yourLabel: UILabel!

    func textViewDidChange(_ textView: UITextView) {
         // Get the word count here
         let wordCount = 10
         // Change the wordCount labels text
         yourLabel.text = "Word Count: \(wordCount)"
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        textView.delegate = self
    }
}

确保您使用CustomCell 中的两个插座。

你的ViewController

class YourViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // Set up the custom Cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
        return cell
    }
}

【讨论】:

  • 感谢您抽出宝贵时间做出如此详细的回答!假设字数标签不在自定义单元格中,您的代码将完美运行。但是,就我而言,确实如此。因此,尝试将标签链接到视图控制器会给我一个错误:“插座无法连接到重复内容”。有什么建议吗?
  • @MansurAhmed 我更新了答案(在 EDIT 下面检查它的底部)。唯一的区别是您不再需要 CustomCellDelegate 协议,并且标签出口将位于 CustomCell 内。如果您仍然无法正常工作,请告诉我。 :)
  • 我采用了您的新代码,但即便如此也没有任何作用。我在 textviewDidChange 函数中添加了一条打印语句,以查看它是否被调用,但事实并非如此。此外,我在单元格创建函数中添加了 'cell.savedHashtagTextView.target(forAction: #selector(HashtagCell.textViewDidChange(_:)), withSender: UIControl.Event.editingChanged)' 以查看是否有帮助,但即使这样也没有'吨。还有什么建议吗? :(
  • 我有其他对象,例如自定义单元格中的按钮,我在其中使用协议和委托来工作,它们非常好,只是标签被证明是如此困难。我还有其他处理一些自动类型功能的 textView 函数,但我将它们放在 ViewController 文件中,它们也可以正常工作。这让我非常难过。
  • @MansurAhmed 在我的代码中,我最初忘记将 UITextView 委托设置为 self。我刚刚更新了它(看看我对CustomCellawakeFromNib() 覆盖所做的更改)。如果您添加它现在应该可以工作。如果没有,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多