我的应用程序中有一个主菜单,其中有15个项目,每个项目都有20个项目的子项,我还将它们添加为图像数组(15个图像数组,类似餐厅菜单之类的东西),因此,当用户单击其中的1个时应用程序会将主菜单项带到子菜单,我已经创建了主菜单表视图,问题在于子菜单,我必须为每个子菜单创建15个表视图!

有没有办法为子菜单创建1个表格视图并根据用户单击更改其数据

注意:我不想在表格视图中使用这些部分

任何想法将不胜感激

最佳答案

  • 需要两个viewControllers和一个navigationController。一个用于主菜单,另一个用于子菜单。让它们成为mainMenuViewController和subMenuViewController。每个控制器都包含一个tableView。
  • 创建一个包含15个子菜单数据的menuArray。每个子菜单都是一个数组。
  • 在mainMenuViewController的didSelectRowAtIndexPath中,如果用户在tableView中选择了一行,则将该数据传递到与所选行相对应的menuArray中。

  • 例如,如果用户选择第三行,
    然后将menuArray [3]传递给subMenuViewController。这里indexPath.row = 3。

    示例项目代码:

    MenuViewController.swift:
    import UIKit
    
    class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        @IBOutlet weak var menuTableView: UITableView!
        var  imagesArray: NSArray = []
        var  menuArray: NSArray = []
        var  subMenuDataArray: NSArray = []
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            menuTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
            imagesArray = ["soups.jpg","salads.jpg","starters.jpg","pizzas.jpg","burgers.jpeg"]
    
            menuArray = ["Soups","Salads","Starters","Pizzas","Burgers"]
    
            subMenuDataArray = [["Cream of broccoli","Cream of celery","Cream of tomato","Etrog","Gazpacho"],
                                ["Tuna salad","Urnebes","Waldorf salad"],
                                ["Kakori Kebabs","Stir Fried Chilli Chicken"," Microwave Paneer Tikkas","Aloo and Dal ki Tikki","Cheese Balls","Bhuna Masala Chicken Wings"],
                                ["Cheese Pizzas","Chicken Pizzas","Masala Pizzas","Double Cheese Pizzas","Herbal Pizzas"],
                                ["Luger Burger","Le Pigeon Burger","The Company Burger","Dyer’s Deep-Fried Burger","The Lola Burger","Cheeseburger","Raw Steak Tartare Burger","Buckhorn Burger"]]
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    
            return imagesArray.count
    
        }
    
        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
            return 70
    
        }
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    
            let  menuTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
            let cellImageView: UIImageView = UIImageView.init()
            cellImageView.frame = CGRectMake(10, 10, 50, 50)
            cellImageView.image =  UIImage(named: imagesArray.objectAtIndex(indexPath.row) as! String)
            menuTableViewCell.contentView.addSubview(cellImageView)
    
            let  menuLabel: UILabel = UILabel.init(frame: CGRectMake(70, 10, 200, 25))
            menuLabel.text = menuArray.objectAtIndex(indexPath.row) as? String
            menuTableViewCell.contentView.addSubview(menuLabel)
    
    
            return menuTableViewCell
        }
    
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    
            let subMenuViewController: SubMenuViewController = storyboard!.instantiateViewControllerWithIdentifier("SubMenuViewControllerID") as! SubMenuViewController
            subMenuViewController.currentSubMenuArray = subMenuDataArray.objectAtIndex(indexPath.row) as! NSArray
            navigationController?.pushViewController(subMenuViewController, animated: true)
    
    
        }
    
    
    }
    

    SubMenuViewController.swift:
    import UIKit
    
    class SubMenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        @IBOutlet weak var subMenuTableView: UITableView!
        var  currentSubMenuArray: NSArray = []
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
    
             subMenuTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subCell")
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
            return currentSubMenuArray.count
        }
    
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
            let  subMenuTableViewCell = tableView.dequeueReusableCellWithIdentifier("subCell", forIndexPath: indexPath)
            let  subMenuLabel: UILabel = UILabel.init(frame: CGRectMake(10, 10, 250, 25))
            subMenuLabel.text = currentSubMenuArray.objectAtIndex(indexPath.row) as? String
            subMenuTableViewCell.contentView.addSubview(subMenuLabel)
            return subMenuTableViewCell
        }
    
    
    }
    

    故事板:

    ios - 如何更改UItableview的数据?-LMLPHP

    输出:

    ios - 如何更改UItableview的数据?-LMLPHP

    要测试示例项目,请使用我的GitHub帐户的以下链接:

    https://github.com/k-sathireddy/MenuTableViewSample

    关于ios - 如何更改UItableview的数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39547673/

    10-12 14:17
    查看更多