如何为 UITableViewCell TextView 对象实现实时字数统计?

How to implement live word count for UITableViewCell TextView object?

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

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

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

中使用该协议

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

作为示例,您将要执行以下操作:

创建一个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 class 并将其 identifier 设置为 "CustomCell"。此外,请确保您 link 您的 UITextViewCustomCell class 中的插座。

现在在 ViewController 其中包含 UITableView:

// 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
    }
}

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

编辑:

根据您的评论,字数统计标签位于单元格中。这意味着我们不再需要 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
    }
}