具有可重复使用的iOS tableview的两级设置菜单
我对iOS应用程序开发非常陌生,需要一些帮助。我正在尝试实现一个设置类屏幕,其中一些项目将在下一个屏幕中显示更多选项。目前正在尝试嵌套的两级设置 我正在尝试使用具有可重复使用的iOS tableview的两级设置菜单,ios,swift,uitableview,code-reuse,Ios,Swift,Uitableview,Code Reuse,我对iOS应用程序开发非常陌生,需要一些帮助。我正在尝试实现一个设置类屏幕,其中一些项目将在下一个屏幕中显示更多选项。目前正在尝试嵌套的两级设置 我正在尝试使用UITableView,但无法将其重新用于显示下一级别的选项。我试图避免在这里重写同一段代码 有人对此有什么建议吗?谢谢 代码如下: class SettingsMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @
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来显示设置页面的两个级别 更改数据源将始终更新单元格内容。因此,您可以使用它在以下位置模拟表中的更改:
optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
只需在上述委托方法中将数据源更改为适当的数据源。(确保新的数据源对象与您在func tableView(u tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell中访问它们的方式兼容)如果您想重用代码,那么您应该推送setingsNuviewController对象(新对象不是自身)使用表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:
}
}
显示您的代码,以了解添加代码的问题到底是什么。感谢@Janit solution也将提供过渡效果供参考