重定向到具有不同数据的同一UIViewController?

时间:2018-07-17 09:22:38

标签: ios swift4

第一个viewController

enter image description here

SecondViewController

enter image description here

FirstViewControllerCode:

     class settingViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    @IBOutlet weak var settingTableview: UITableView!
    var settingArray = [settingClass(Name:["General","Privous"],image:["genral","previous"]),
                    settingClass(Name:["Maps","Safari","News","Siri","Photos","GameCenter"],image:["map","safari","news","siri","photos","game"]),
                    settingClass(Name:["Developer"],image:["developer"])
                    ]

func numberOfSections(in tableView: UITableView) -> Int {
    return settingArray.count
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return settingArray[section].Name.count    
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 55
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 30
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let setCell = settingTableview.dequeueReusableCell(withIdentifier: "SettingCell", for: indexPath) as! settingCell

    setCell.imageView?.image = UIImage(named: settingArray[indexPath.section].image[indexPath.row])
    setCell.settingLabel.text = settingArray[indexPath.section].Name[indexPath.row]

    return setCell
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let  vc = self.storyboard?.instantiateViewController(withIdentifier: "descriptionId") as! descriptionViewController
   // vc.arr = descriptionArray[indexPath.section].Name[indexPath.row]

    vc.descriptionArray = [descriptionClass(Name:["About"]),
    descriptionClass(Name:["Language","Dictionary","Bluetooth"]),
    descriptionClass(Name:["Reset"])]

    self.present(vc, animated: true, completion: nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Settings"
    navigationItem.backBarButtonItem = backItem
  }

 }

SecondViewController代码:

    class descriptionViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

   @IBOutlet weak var descriptionTableview: UITableView!

var descriptionArray = [descriptionClass]()

func numberOfSections(in tableView: UITableView) -> Int {
    return descriptionArray.count
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 40
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return descriptionArray[section].Name.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 60
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let descCell = descriptionTableview.dequeueReusableCell(withIdentifier: "descriptionCell", for: indexPath) as! descriptionCell
    descCell.descLabel.text = descriptionArray[indexPath.section].Name[indexPath.row]
    return descCell
}
}

我的问题是:当我单击FirstViewController的任何行(常规,上一个,地图,siri等)时,它应该移动到具有不同数据的SecondViewController。就像iPhone中的“设置”应用程序。以及如何设计didSelectRowAt()。请帮助我

  

我的故事板结构:

enter image description here

3 个答案:

答案 0 :(得分:1)

您需要实施

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let vc = self.storyboard?.instantiateViewController(withIdentifier: "descriptionId") as! descriptionViewController 
     // if you want to send data use  descriptionArray[indexPath.section]
     self.navigationController?.pushViewController(vc, animated:true)
}

并设置

self.tableView.delegate = self

答案 1 :(得分:0)

在视图控制器中添加一个按钮。从您的按钮创建一个Segue到相同的ViewController。这里的按钮仅用于创建(自)序列,您可以隐藏该按钮。故事板中将显示以下内容:

enter image description here

在您的代码中,您可以通过调用以下内容导航到视图控制器:

self.performSegue(withIdentifier: "SegueSelf", sender: self)

答案 2 :(得分:0)

您应该使用@HAK的答案执行segue并将此代码添加到settingsViewController中以发送数据。

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
         if let dest = destination as? descriptionViewController{
              dest.descriptionArray = //Your detailed data
         }
    }

注意:您可能需要用tableView.reloadData()的{​​{1}}方法写descriptionViewController