如何为字符串中的每个字符向 TableView 添加一个新单元格?

How to add a new cell to a TableView for each character in a string?

我正在尝试从 TextView 中获取一个字符串并为 TextView.text 中的每个字符添加一个新行。

所以如果 TextView text = "hi there" 例如,我希望 Table 视图看起来像这样:

h

(space)

t

h

e

r

e


谢谢!

当用户输入新文本时,将您的字符串转换为字符数组:

let chars = Array(string)

使用字符数组作为 table 视图的数据源。更改字符数组后,在 table 视图上调用 reloadData()

您的数据源方法可能如下所示:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return chars.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = String(chars[indexPath.row])
    return cell
}

编辑:

下面是 UITableViewController 的一个完全实现的子class。

要使用它:

  1. 将 class 作为 Swift 文件添加到您的项目中。

  2. 将 UITableViewController 场景添加到故事板

  3. Select "Identity Inspector" 并将 class 更改为 CharacterTableViewController

  4. 将一个容器视图拖到你想要的视图控制器上 包含您的 table 视图。 (我将其称为 "parent" 视图 控制器)

  5. 按住 Control 从容器视图拖动到您的 CharacterTableViewController。在出现的弹出菜单中,select "embed" 创建嵌入转场。

  6. 向您的父级添加一个 prepare(for:sender:) (prepareForSegue) 方法 尝试将 segue.destination 转换为类型的视图控制器 CharacterTableViewController 使用 if let,如果成功,将 CharacterTableViewController 保存到实例变量。 (我们称它为 characterVC。)

  7. 将文本视图添加到您的父视图控制器。控制拖动到您的父视图控制器以将 IBOutlet 添加到文本视图。呼叫出口 theTextView.

  8. 向父视图控制器添加一个按钮。按住 Control 从按钮拖动到父视图控制器以创建 IBAction。在按钮操作中,从 theTextView 获取文本并将其传递给 characterVC (characterVC.contentString = theTextView.text)


import UIKit

class CharacterTableViewController: UITableViewController {

    public var contentString: String? {
        didSet {
            if let string = contentString {
                characters = Array(string)
            } else {
                characters = nil
            }
        }
    }

    var characters: [Character]? {
        didSet {
            tableView.reloadData()
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return characters?.count ?? 0
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = String(characters?[indexPath.row] ?? Character(""))
        return cell
    }
}

编辑#2:

我在演示项目中创建了一个使用上述 table 视图 class 的项目。您可以在以下link下载:https://github.com/DuncanMC/CharacterTableView.git

这个比较直接。希望大家喜欢

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return textView.text.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let myText = textView.text
    cell.textLabel?.text = "\(myText[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
    return cell
}

有错字。我修好了,给你看结果。