iOS 具有可重复使用的表格视图的两级设置菜单
Two level settings menu with reusable tableview for iOS
我是 iOS 应用程序开发的新手,需要一些帮助。我正在尝试实现一种设置屏幕,其中某些项目将在下一个屏幕中显示更多选项。现在尝试嵌套两级设置。
我正在尝试使用 UITableView
但无法重复使用它来显示下一级选项。我试图避免在这里重写同一段代码。
有人对此有什么建议吗?谢谢
代码如下:
class SettingsMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var Options = ["Settings1", "Settings2", "Settings3" ]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Options.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel!.text = Options[indexPath.row]
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
switch cell?.textLabel?.text {
case "Settings1"?:
//On selecting this option, need to load new set of setting options which are relevant to Settings1 but want to reuse the tableView available here
case "Settings2"?:
case "Settings3"?:
case "Settings4"?:
default:
}
}
}
您似乎在尝试使用相同的 tableView 来显示设置页面的两个级别。
更改数据源将始终更新单元格内容。因此,您可以在以下位置使用它来模拟 table 的变化:
optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
只需在上述委托方法中将数据源更改为合适的数据源即可。 (确保新数据源对象与您在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 中访问它们的方式兼容 -> UITableViewCell)
如果你想重用代码,那么你应该推送 SettingsMenuViewController 对象(新对象不是自己),选项值在 table didSelectRowAt 就像:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
switch cell?.textLabel?.text {
case "Settings1"?:
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingsMenuViewController") as! SettingsMenuViewController
vc.Options = ["Settings5","Settings6"]
self.navigationController?.pushViewController(vc, animated: true)
case "Settings2”?:
case "Settings3”?:
case "Settings4”?:
default:
}
}
我是 iOS 应用程序开发的新手,需要一些帮助。我正在尝试实现一种设置屏幕,其中某些项目将在下一个屏幕中显示更多选项。现在尝试嵌套两级设置。
我正在尝试使用 UITableView
但无法重复使用它来显示下一级选项。我试图避免在这里重写同一段代码。
有人对此有什么建议吗?谢谢
代码如下:
class SettingsMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var Options = ["Settings1", "Settings2", "Settings3" ]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Options.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel!.text = Options[indexPath.row]
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
switch cell?.textLabel?.text {
case "Settings1"?:
//On selecting this option, need to load new set of setting options which are relevant to Settings1 but want to reuse the tableView available here
case "Settings2"?:
case "Settings3"?:
case "Settings4"?:
default:
}
}
}
您似乎在尝试使用相同的 tableView 来显示设置页面的两个级别。
更改数据源将始终更新单元格内容。因此,您可以在以下位置使用它来模拟 table 的变化:
optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
只需在上述委托方法中将数据源更改为合适的数据源即可。 (确保新数据源对象与您在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 中访问它们的方式兼容 -> UITableViewCell)
如果你想重用代码,那么你应该推送 SettingsMenuViewController 对象(新对象不是自己),选项值在 table didSelectRowAt 就像:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
switch cell?.textLabel?.text {
case "Settings1"?:
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingsMenuViewController") as! SettingsMenuViewController
vc.Options = ["Settings5","Settings6"]
self.navigationController?.pushViewController(vc, animated: true)
case "Settings2”?:
case "Settings3”?:
case "Settings4”?:
default:
}
}