如何将Table View Controller与多个子类别一起使用 [英] How to use Table View Controller with multiple sub categories

查看:91
本文介绍了如何将Table View Controller与多个子类别一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,我想添加几个类别.

I have a UITableView with and I want to add several categories.

我的数据看起来像这样:

My data looks somewhat like this:

  • 主要类别A
    • 子类别红色
      • SubSubCategory X
        • SubSubSubCategory
        • MainCategory A
          • SubCategory Red
            • SubSubCategory X
              • SubSubSubCategory
              • 子类别红色
                • SubSubCategory X
                • SubSubCategory Y
                • SubCategory Red
                  • SubSubCategory X
                  • SubSubCategory Y
                  • SubSubCategory X

                  第一个屏幕应该显示所有MainCategories,然后如果我单击A,它应该显示SubCategory红色和蓝色,依此类推...

                  The first screen should show all the MainCategories, then if I click on A, it should show me SubCategory Red and Blue, and so on...

                  我到处搜索,但找不到解决方案.

                  I searched everywhere but couldn't find a solution.

                  我应该如何构造我的数据,这样才能正常工作? cellForRowAt仅使您可以选择提供第一组子类别的数据.我将其余的数据放在哪里?

                  How should I structure my data so that this would work? cellForRowAt only gives you the option to give the data for the first set of sub categories. Where do I put the data for the rest?

                  推荐答案

                  创建带有标题和Menu对象数组的结构菜单.

                  Create a struct Menu with a title and an array of Menu objects.

                  struct Menu {
                      var title: String
                      var subMenus: [Menu]?
                  }
                  

                  在要启动菜单的视图控制器中添加菜单详细信息.并使用菜单对象推送UITableViewController.如果按下了子菜单,则按UITableViewController和子菜单对象.

                  Add menu detail in a view controller where you want to start the menu. And push a UITableViewController with the menu object. If a submenu is pressed push a UITableViewController with the sub menu object.

                  //ViewController.swift

                  //ViewController.swift

                  class ViewController: UIViewController {
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                      }
                      @IBAction func startMenuAction(_ sender: UIButton) {
                          let red1 = Menu(title: "SubCategory Red", subMenus: [Menu(title: "SubSubCategory X", subMenus: [Menu(title: "SubSubSubCategory", subMenus: nil)]),
                                                                               Menu(title: "SubSubCategory Y", subMenus: nil)])
                          let blue1 = Menu(title: "SubCategory Blue", subMenus: nil)
                  
                          let red2 = Menu(title: "SubCategory Red", subMenus: [Menu(title: "SubSubCategory X", subMenus: nil),
                                                                               Menu(title: "SubSubCategory Y", subMenus: nil)])
                          let blue2 = Menu(title: "SubCategory Blue", subMenus: [Menu(title: "SubSubCategory X", subMenus: nil)])
                          let green2 = Menu(title: "SubCategory Green", subMenus: nil)
                  
                          let categories = [Menu(title: "MainCategory A", subMenus: [red1, blue1]), Menu(title: "MainCategory B", subMenus: [red2, blue2, green2])]
                  
                          let mainMenu:Menu = Menu(title: "My Menu", subMenus: categories)
                  
                          let menuVC = MenuVC()
                          menuVC.currentMenu = mainMenu
                          self.navigationController?.pushViewController(menuVC, animated: true)
                      }
                  }
                  

                  //MenuVC.swift

                  //MenuVC.swift

                  class MenuVC: UITableViewController {
                      var currentMenu: Menu?
                      override func viewDidLoad() {
                          super.viewDidLoad()
                          view.backgroundColor = .white
                          title = currentMenu?.title
                      }
                  
                      // MARK: - Table view data source
                      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                          return (currentMenu?.subMenus?.count ?? 0)
                      }
                  
                      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                          let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") ?? UITableViewCell(style: .default, reuseIdentifier: "reuseIdentifier")
                          if currentMenu?.subMenus?[indexPath.row].subMenus?.isEmpty ?? true {
                              cell.accessoryType = .none
                          } else {
                              cell.accessoryType = .disclosureIndicator
                          }
                          cell.textLabel?.text = currentMenu?.subMenus?[indexPath.row].title
                          return cell
                      }
                      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                          if !(currentMenu?.subMenus?[indexPath.row].subMenus?.isEmpty ?? true) {
                              let menuVC = MenuVC()
                              menuVC.currentMenu = currentMenu?.subMenus?[indexPath.row]
                              self.navigationController?.pushViewController(menuVC, animated: true)
                          } 
                      }
                  }
                  

                  这篇关于如何将Table View Controller与多个子类别一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆