如何从UITableView创建菜单,子菜单数量未知?

时间:2016-07-04 13:53:42

标签: ios swift uitableview nsdictionary

如何在Swift中将UITableView显示为多级菜单

我正在尝试将多级菜单显示为一系列UITableView s,其中每个对象可能包含未知数量的子项,然后可能 n 孩子,等等。

我正在尝试使用闭包回调(下面),但是一旦我进入菜单层次结构两层,就会遇到问题。

相关代码:

TableViewController

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var selectedObject:Dictionary<String, AnyObject>

    if (self.hasChildren) {
       selectedObject  = self.childrenObjects![indexPath.row]
    } else {
        selectedObject = self.tableViewData![indexPath.row]
    }


    self.selectionCallback!(tableView: tableView, indexPath: indexPath, selectedObject: selectedObject, doesHaveChildren: self.hasChildren)
}

MenuViewController

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

    let menuTableViewController:TableViewBuilder = TableViewBuilder()
    menuTableViewController.tableViewData = self.passedData as Array<Dictionary<String, AnyObject>>?

    self.navController = UINavigationController(rootViewController: menuTableViewController)


    self.presentViewController(self.navController!, animated: false) {
        print("Nav controller presented")
        //navController.pushViewController(menuTableViewController, animated: false)
    }

    self.selectedTapCallback = { (tableView, indexPath, selectedObject, doesHaveChildrenTop) in

        let detailMenuViewController:TableViewBuilder = TableViewBuilder()
        detailMenuViewController.tableViewData = selectedObject["children"] as! StandardCollectionType
        self.navController!.pushViewController(detailMenuViewController, animated: true)
    }



    menuTableViewController.setSelectedCallback(self.selectedTapCallback!)


}

我试图表示的数据如下:

    "menu": [
                    {
                        "title": "Jurisdiction",
                        "subtitle": "Where did you get the ticket?",
                        "has_children": true,
                        "meta": {
                            "top_level_menu": true
                        },

                        "children": [{
                            "title": "New South Wales",
                            "subtitle": "Seeking review with the Office of State Revenue (SDRO)",
                            "has_children": true,
                            "meta": {
                                "option_uid": "nsw"
                            },

                            "children": [{
                                    "title": "Car registration permit date inccorect",
                                    "subtitle": null,
                                    "has_children": false,
                                    "meta": {
                                        "option_uid": "nsw.permit.date.inccorect"
                                    }
                                },

                                {
                                    "title": "There was an issue with the signs",
                                    "subtitle": "This could be due to a number of reasons",
                                    "has_children": true,
                                    "children": [{
                                            "title": "The signs were contradictory",
                                            "subtitle": null,
                                            "has_children": false,
                                            "meta": {
                                                "option_uid": "nsw.bay.signs.contradictory"
                                            }
                                        },

                                        {
                                            "title": "The signs were unclear",
                                            "subtitle": "The signs may have been unclear or ambiguous",
                                            "has_children": false,
                                            "meta": {
                                                "option_uid": "nsw.bay.signs.unclear"
                                            }
                                        }
                                    ]
                                },

                                {
                                    "title": "The offence happened after I sold the car",
                                    "subtitle": "It wasn't me!",
                                    "has_children": false,
                                    "meta": {
                                        "option_uid": "nsw.car.after.sold"
                                    }
                                }

我正在尝试通过使用由- tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)触发的闭包回调来实例化另一个MenuViewController实例(如上面的代码中所示),然后让主导航控制器推送它,但是当试图从第二级移动到菜单的第三级时,这种情况就会破裂。

任何有关处理此问题的最佳方式的想法都将受到赞赏!

1 个答案:

答案 0 :(得分:0)

我开发了类似的东西,我使用了这个library

希望这可以帮助你。