Ios 重用具有动态单元格的第二个视图控制器以在Swift中显示不同数组的元素是否有效?

Ios 重用具有动态单元格的第二个视图控制器以在Swift中显示不同数组的元素是否有效?,ios,swift,uiviewcontroller,Ios,Swift,Uiviewcontroller,我目前有2个表视图控制器。我在婚姻状况和家庭状况(canton)的两个静态单元格中添加了两个披露指标。用户单击两个视图中的一个,并被带到另一个视图控制器,在该控制器中进行适当的选择 该准则目前适用于婚姻状况。我的问题是,在这里我是否可以重用第二个视图控制器(即带有动态单元格的控制器),用于相同的目的,但使用不同的数组(在本例中是带有状态名称的数组)。对我来说,很明显,我可以简单地添加一个新的视图控制器并在那里实现状态列表。以下是故事板的屏幕截图: 第一视图控制器代码: import UIKit

我目前有2个
表视图控制器
。我在婚姻状况和家庭状况(canton)的两个静态单元格中添加了两个披露指标。用户单击两个视图中的一个,并被带到另一个视图控制器,在该控制器中进行适当的选择

该准则目前适用于婚姻状况。我的问题是,在这里我是否可以重用第二个视图控制器(即带有动态单元格的控制器),用于相同的目的,但使用不同的数组(在本例中是带有状态名称的数组)。对我来说,很明显,我可以简单地添加一个新的视图控制器并在那里实现状态列表。以下是故事板的屏幕截图:

第一视图控制器代码:

import UIKit

class FirstTableViewController: UITableViewController, DataEnteredDelegate {

    @IBOutlet var maritalStatusCell: UITableViewCell!
    @IBOutlet var maritalStatusLabel: UILabel!



    func userDidEnterInformation(info: String) {

    maritalStatusLabel.text = "Marital Status: (\(info))"

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "maritalStatusSegue" {
            let sendingVC: SecondTableViewController = segue.destination as! SecondTableViewController
            sendingVC.delegate = self
        }
    }

}
import UIKit

protocol DataEnteredDelegate {
    func userDidEnterInformation(info: String)
}

class SecondTableViewController: UITableViewController {
    let maritalStatusArray: [String] = ["Single", "Married"]
    let cantonArray: [String] = ["ZG", "ZH", "BE", "LU", "AG"]
    var delegate: DataEnteredDelegate? = nil





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

    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if delegate != nil {
            let information: String? = tableView.cellForRow(at: indexPath)?.textLabel?.text
            delegate!.userDidEnterInformation(info: information!)
            dismiss(animated: true, completion: nil)
            self.navigationController?.popViewController(animated: true)
        }


    }




    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

    }



}
第二视图控制器代码:

import UIKit

class FirstTableViewController: UITableViewController, DataEnteredDelegate {

    @IBOutlet var maritalStatusCell: UITableViewCell!
    @IBOutlet var maritalStatusLabel: UILabel!



    func userDidEnterInformation(info: String) {

    maritalStatusLabel.text = "Marital Status: (\(info))"

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "maritalStatusSegue" {
            let sendingVC: SecondTableViewController = segue.destination as! SecondTableViewController
            sendingVC.delegate = self
        }
    }

}
import UIKit

protocol DataEnteredDelegate {
    func userDidEnterInformation(info: String)
}

class SecondTableViewController: UITableViewController {
    let maritalStatusArray: [String] = ["Single", "Married"]
    let cantonArray: [String] = ["ZG", "ZH", "BE", "LU", "AG"]
    var delegate: DataEnteredDelegate? = nil





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

    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if delegate != nil {
            let information: String? = tableView.cellForRow(at: indexPath)?.textLabel?.text
            delegate!.userDidEnterInformation(info: information!)
            dismiss(animated: true, completion: nil)
            self.navigationController?.popViewController(animated: true)
        }


    }




    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

    }



}

在这里,对状态列表使用第二个表视图控制器有意义吗?如果是,我如何实施?谢谢。

是的,您可以使用相同的视图控制器来显示州名称数组,我想您已经在
cantonArray
中声明了,您需要做的是在
第二视图控制器中声明一个bool变量(如果您只想管理两个数组,如果您想管理更多数组,那么就声明一个
enum
)。然后在segue-get中,从哪个索引触发segue,您可以像这样获取所选的indexath

if let indexPath = tableView.indexPathForSelectedRow{

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

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

    } else {
          cell.textLabel?.text = cantonArray[indexPath.row]

    }
    return cell

}
现在检查indexPath.row,如果它是
0
,那么您已经选择了婚姻状态,因此需要显示
maritalStatusArray
数组,因此使bool变量
true
如果您得到
indexPath.row=1
则使该变量
false

现在,在第二个视图中,控制器根据bool变量添加一个条件,并如下所示显示来自该数组的数据

if let indexPath = tableView.indexPathForSelectedRow{

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

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

    } else {
          cell.textLabel?.text = cantonArray[indexPath.row]

    }
    return cell

}
这就是如何声明
enum

enum SelectedRow {
    case MaritalStatus
    case States
    case ThirdRow
}

var selectedRow = SelectedRow.MaritalStatus

你能在你的回答中添加一个使用枚举的例子吗?嗨,拉贾特,谢谢你的回答。因为我是Swift的初学者,我不确定我是否理解你所说的。所以布尔变量是showMaritalArray?我不明白如果'let indexath=tableView.indexPathForSelectedRow{},'。。。。