[英]How to add a new cell to a TableView for each character in a string?
I am trying to take a string from a TextView and add a new row for each character in TextView.text. 我试图从TextView中获取一个字符串,并为TextView.text中的每个字符添加一个新行。
so if the TextView text = "hi there" for example, I would like the Table View to look like this: 因此,例如,如果TextView text =“ hi there”,我希望Table View看起来像这样:
h H
i 一世
(space) (空间)
t Ť
h H
e Ë
r [R
e Ë
thank you! 谢谢!
When the user enters new text convert your string into an array of characters: 当用户输入新文本时,将您的字符串转换为字符数组:
let chars = Array(string)
Use the array of caracters as the data source for your table view. 将角色数组用作表视图的数据源。 Call
reloadData()
on your table view after changing the character array. 更改字符数组后,在表视图上调用
reloadData()
。
Your data source methods might look like this: 您的数据源方法可能如下所示:
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
}
Below is a fully implemented subclass of UITableViewController. 下面是UITableViewController的完全实现的子类。
To use it: 要使用它:
Add the class to your project as a Swift file. 将类作为Swift文件添加到您的项目中。
Add a UITableViewController scene to your storyboard 将UITableViewController场景添加到情节提要
Select the "Identity Inspector" and change the class to CharacterTableViewController
选择“身份检查器”,并将类更改为
CharacterTableViewController
Drag a container view onto the view controller that you want to contain your table view. 将容器视图拖到要包含表视图的视图控制器上。 (I'll call this the "parent" view controller)
(我将其称为“父”视图控制器)
Control-drag from the container view to your CharacterTableViewController
. Control从容器视图拖动到
CharacterTableViewController
。 In the resulting pop-up menu, select "embed" to create an embed segue. 在出现的弹出菜单中,选择“嵌入”以创建嵌入序列。
Add a prepare(for:sender:)
(prepareForSegue) method to your parent view controller that tries to cast segue.destination to type CharacterTableViewController
using an if let
and if it succeeds, save the CharacterTableViewController
to an instance variable. 向您的父视图控制器中添加一个
prepare(for:sender:)
(prepareForSegue)方法,该方法尝试使用if let
如果成功,则将segue.destination转换为CharacterTableViewController
类型,将CharacterTableViewController
保存到实例变量。 (Let's call it characterVC
.) (我们称它为
characterVC
。)
Add a text view to your parent view controller. 将文本视图添加到父视图控制器。 Control drag into your parent view controller to add an
IBOutlet
to the text view. 将控件拖到您的父视图控制器中,以将
IBOutlet
添加到文本视图。 Call the outlet theTextView
. 致电出口
theTextView
。
Add a button to the parent view controller. 将一个按钮添加到父视图控制器。 Control-drag from the button into your parent view controller to create an IBAction.
按住Control键从按钮中拖动到父视图控制器中,以创建IBAction。 In the button action, fetch the text from
theTextView
and pass it to characterVC
( characterVC.contentString = theTextView.text
) 在按钮操作中,从
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
}
}
I created a project that uses the table view class above in a demo project. 我创建了一个在演示项目中使用上面的表视图类的项目。 You can download it at the following link: https://github.com/DuncanMC/CharacterTableView.git
您可以从以下链接下载它: https : //github.com/DuncanMC/CharacterTableView.git
This one is more directly. 这个更直接。 Hope you like it.
希望你喜欢。
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
}
There was a typo. 有一个错字。 I fixed it and show you the result.
我将其修复并向您显示结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.