【问题标题】:How to exchange information between 2 table view controllers in Swift? [duplicate]如何在 Swift 中的 2 个表视图控制器之间交换信息? [复制]
【发布时间】:2017-04-15 22:51:47
【问题描述】:

我是 Swift 新手。我有两个table view controllers,一个带有静态单元格,一个带有动态单元格。我基本上想让用户在第二个表格视图控制器上选择他的婚姻状态并将他的选择发送回第一个表格视图控制器(并在单元格“婚姻状态”上显示他的选择)。这是我的故事板的屏幕截图:

Storyboard

第二个表格视图控制器上的当前代码:

import UIKit

class SecondTableViewController: UITableViewController {
     let maritalStatusArray: [String] = ["Single", "Married"]

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

    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "MaritalStatusCell", for: indexPath)
        cell.textLabel?.text = maritalStatusArray[indexPath.row]
        return cell

    }
}

我的猜测是我必须将第二个视图控制器的动态单元格中的一个转场添加回第一个。那正确吗 ?

假设这是执行此操作的方法,之后我必须更新静态标签的文本以包含用户所做的选择。有什么想法吗?

【问题讨论】:

    标签: ios swift viewcontroller


    【解决方案1】:

    您可以使用自定义委托来执行此操作:

    ViewController.swift:

    import UIKit
    
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SecondViewControllerProtocol {
    
        @IBOutlet weak var userInfoTableView: UITableView!
    
        var userInfoArray: [String] = []
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            userInfoArray = ["Marital Status","Canton","Commune","Religion"]
           self.userInfoTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 4
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let userInfoCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            userInfoCell.textLabel?.text = userInfoArray[indexPath.row]
            if indexPath.row == 0{
                userInfoCell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
            }
            return userInfoCell
        }
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if indexPath.row == 0{
                let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewControllerIdentifier") as! SecondViewController
                secondVC.statusDelegate = self
                self.navigationController?.present(secondVC, animated: true, completion: nil)
            }
        }
    
        func changeMaritalStatus(type: String){
            let  maritalStatusCell = userInfoTableView.cellForRow(at: IndexPath(row:0 , section:0))
            maritalStatusCell?.textLabel?.text = String("Marital Status: \(type)")
        }
    }
    

    SecondViewController.swift:

    import UIKit
    
    protocol SecondViewControllerProtocol {
        func changeMaritalStatus(type: String)
    }
    
    class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        @IBOutlet weak var maritalStatusTableView: UITableView!
    
        var maritalStatusArray: [String] = []
    
        var statusDelegate  : SecondViewControllerProtocol? = nil
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
            maritalStatusArray = ["Single","Married","Divorced"]
            self.maritalStatusTableView.register(UITableViewCell.self, forCellReuseIdentifier: "maritalStatuscell")
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 3
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let maritalStatusInfoCell = tableView.dequeueReusableCell(withIdentifier: "maritalStatuscell", for: indexPath)
            let infoLabel: UILabel = UILabel.init(frame: CGRect(x: 10, y: 5, width: 250, height: 50))
            infoLabel.text = maritalStatusArray[indexPath.row]
            maritalStatusInfoCell.addSubview(infoLabel)
            return maritalStatusInfoCell
        }
    
         func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            statusDelegate?.changeMaritalStatus(type: maritalStatusArray[indexPath.row])
            self.dismiss(animated: true, completion: nil)
        }
    
    }
    

    GitHub 链接:

    https://github.com/k-sathireddy/CustomDelegatesSwift

    输出:-

    【讨论】:

      【解决方案2】:

      您可以通过几种方式实现回调功能来传递数据。

      1. 委托
      2. 使用块回调
      3. 发布通知

      但我建议使用委托,这是最好的方式,发布通知也是一种方式,但我不想更喜欢。

      【讨论】:

        猜你喜欢
        • 2012-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多