首页 > 解决方案 > 使用导航按钮从 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)")
        
    }

标签: swiftuitableview

解决方案


使用自定义结构来维护选定的状态。这是最方便的方法

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

推荐阅读