swift - 使用导航按钮从 UITableView 中的选中元素返回数据
问题描述
我有一个 UITableView,上面显示了一些元素。
如果用户单击任何元素,则可以检查文本字段上的元素。
我有这方面的代码,它工作得很好。
class MyTableViewController: UITableViewController {
let foods = ["tomato", "cheese", "bread", "apple"]
let cellReuseIdentifier = "cell"
@IBOutlet weak var myTableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "DONE", style: .done, target: self, action: #selector(self.avancar(sender:)))
}
@objc func avancar(sender: UIBarButtonItem) {
print("DONE button clicked")
print("myTableView.numberOfSections: \(myTableView.numberOfSections)")
print("myTableView.numberOfRows: \(myTableView.numberOfRows(inSection: 0))")
for i in 0..<foods.count {
if tableView.cellForRow(at: [0, i])?.accessoryType == UITableViewCell.AccessoryType.none {
print("item \(i) is UNchecked")
} else {
print("item \(i) is checked")
}
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foods.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
cell.textLabel?.text = foods[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("x: \(foods[indexPath.row])")
print(indexPath)
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCell.AccessoryType.checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.none
} else {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.checkmark
}
}
}
但我现在想要的是返回一个字符串,其中包含检查到 UIViewController 内的 UITextField 的所有元素。
我想对其进行以下行为,当用户单击 TextField 时,必须将用户重定向到 UITableView,该 UITableView 显示可以检查的元素。
在用户单击 UITableView 的任意数量的元素后,用户将单击 DONE 按钮,该按钮将获取所有选中元素的文本(字符串)并将其返回给 UITextField。
如何将字符串返回到 UIViewController 中的 UITextField。
您可以在下面找到我创建的屏幕图像。
编辑
我放了另一个屏幕截图来显示我的代码现在正在做什么。
.
我想返回选中的文本,而不是选中的文本。
另一个编辑
我能够使用下面的代码获得检查元素的 String 结果,现在我仍然需要知道如何将数据返回到以前的 UIViewController
@objc func avancar() {
textToReturn = ""
for i in 0..<foods.count {
if tableView.cellForRow(at: [0, i])?.accessoryType == UITableViewCell.AccessoryType.none {
print("item \(i) is UNchecked")
} else {
textToReturn = textToReturn + foods[i] + ", "
}
}
print("textToReturn: \(textToReturn)")
}
解决方案
使用自定义结构来维护选定的状态。这是最方便的方法
struct Food {
let name : String
var isSelected = false
}
声明数据源
let foods = [Food(name:"tomato"),
Food(name:"cheese"),
Food(name:"bread"),
Food(name:"apple")]
在cellForRow
根据isSelected
属性设置附件视图
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foods.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
cell.textLabel?.text = foods[indexPath.row].name
cell.accessoryType = foods[indexPath.row].isSelected ? .checkmark : .none
return cell
}
在didSelect
切换isSelected
数据源中的属性并重新加载行
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("x: \(foods[indexPath.row])")
foods[indexPath.row].isSelected.toggle()
tableView.reloadRows(at: [indexPath], with: .none)
}
在 action 方法filter
中选择的项目,map
他们到他们的名字和join
他们到一个字符串。
@objc func avancar(sender: UIBarButtonItem) {
print("DONE button clicked")
let selectedItems = foods.filter{$0.isSelected}.map{$0.name}.joined(separator: ", ")
print(selectedItems)
}
推荐阅读
- c# - 如何在 Unity 中向左滑动按钮?
- autodesk-forge - 当我们上传 .rvt 文件时,我们总是收到错误消息“TranslationWorker-InternalFailure”。文件没有损坏,我们已经检查过了
- python - Matplot Numpy ValueError:使用序列设置数组元素
- android - 如何使用 Coroutines android 执行任务
- facebook - 使用 Graph API 获取 Facebook 主页帖子
- raspberry-pi3 - Yocto,Meta-selinux 不适用于树莓派 3
- r - 将回归结果保存在乳胶和文本中
- python - 有没有Django默认认证数据库模型的实体关系图?
- c - 计算 C 中的 searies 的几何平均值
- php - 如何在 Laravel 5.5 的自定义验证中传递数组