第一个viewController
SecondViewController
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()。请帮助我
我的故事板结构:
最佳答案
您需要实施
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
关于ios - 重定向到具有不同数据的同一UIViewController?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51377685/