我有两个UITableViewController
:SettingsViewController
和DetailsViewController
。我想要一个Details
控制器用于不同的Settings
,即"语言","货币","可见性"等
所以,我需要在DetailsViewController
知道SettingsViewController
的哪个单元格打开它。
我在Swift方面还不是很好,所以我在Interface Builder中为SettingsViewController
构建了单元格,我的代码中没有cellForRowAtIndexPath
方法。我需要实现这种方法吗?或者,也许,我可以通过索引(或标签名称)获取特定单元格?但为此,我需要将整个SettingsViewController
连接到DetailsViewController
,我也不知道该怎么做。
事实上我在DetailsViewController
:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
# if it was called by "Language" cell
return 2
# if it was called by "Currency" cell
return 5
# else
return 7
}
感谢您的帮助!如果您需要这些控制器的代码,我会添加它,但事实上它没有什么特别之处,只有标准return
s的标准方法。
答案 0 :(得分:0)
你必须实现prepareForSegue:sender:
方法,因为@dasdom如下所示:
// SettingsViewController.swift
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Storyboard Segue Identifier" {
// then you get index with `indexPathForSelectedRow`
if let indexPath = self.tableView.indexPathForSelectedRow() {
let controller = segue.destinationViewController as DetailsViewController
// configure the controller
// controller.xxx = xxx
}
}
}
答案 1 :(得分:0)
您可以使用tableView的didSelectRowAtIndexPath和prepareForSegue:
let data = ["Language", "Currency", "Visibility"]
var dvcRowCount:Int?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch indexPath.row {
case 0: dvcRowCount = 2
case 1: dvcRowCount = 3
case 2: dvcRowCount = 4
default: dvcRowCount = 1
}
performSegueWithIdentifier("showDetail", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let dvc = segue.destinationViewController as DetailTableViewController
dvc.rowCount = dvcRowCount
}
在detailTableViewController中:
var rowCount:Int?
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return 0 if the rowCount is not set
return rowCount ?? 0
}
我上传了演示代码:https://github.com/vidaaudrey/SharingDetailTableView