Return 使用导航按钮从 UITableView 中选中的元素中获取数据
Return data from checked elements in UITableView using Navigation button
我有一个显示一些元素的 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
}
}
}
但我现在想要的是 return 一个包含所有元素的字符串,这些元素被检查到 UIViewController 中的 UITextField。
我想在上面添加以下行为,当用户单击 TextField 时,必须将用户重定向到显示可以检查它的元素的 UITableView。
用户点击 UITableView 的任意数量的元素后,用户将点击 DONE 按钮,将 return 中所有选中元素的文本(字符串)获取到 UITextField。
我怎样才能 return 一个字符串到 UIViewController 中的 UITextField。
您可以在下面找到我创建的屏幕图像。
编辑
我放了另一个屏幕截图来显示我的代码现在正在做什么。
.
我要return选中的文字,没有选中。
另一个编辑
我能够使用下面的代码得到检查元素的字符串结果,现在我仍然需要知道如何return 数据到以前的 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)
}
在操作方法中 filter
选定的项目,map
它们的名称和 join
它们的一个字符串。
@objc func avancar(sender: UIBarButtonItem) {
print("DONE button clicked")
let selectedItems = foods.filter{[=14=].isSelected}.map{[=14=].name}.joined(separator: ", ")
print(selectedItems)
}
我有一个显示一些元素的 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
}
}
}
但我现在想要的是 return 一个包含所有元素的字符串,这些元素被检查到 UIViewController 中的 UITextField。
我想在上面添加以下行为,当用户单击 TextField 时,必须将用户重定向到显示可以检查它的元素的 UITableView。
用户点击 UITableView 的任意数量的元素后,用户将点击 DONE 按钮,将 return 中所有选中元素的文本(字符串)获取到 UITextField。
我怎样才能 return 一个字符串到 UIViewController 中的 UITextField。
您可以在下面找到我创建的屏幕图像。
编辑
我放了另一个屏幕截图来显示我的代码现在正在做什么。
我要return选中的文字,没有选中。
另一个编辑
我能够使用下面的代码得到检查元素的字符串结果,现在我仍然需要知道如何return 数据到以前的 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)
}
在操作方法中 filter
选定的项目,map
它们的名称和 join
它们的一个字符串。
@objc func avancar(sender: UIBarButtonItem) {
print("DONE button clicked")
let selectedItems = foods.filter{[=14=].isSelected}.map{[=14=].name}.joined(separator: ", ")
print(selectedItems)
}