简体   繁体   中英

How to create a Hierarchical menu in iOS?

In my application, I have a menu that is defined as follows. Each item on the menu is a node that has the following data attributes:

MenuText : (the text that would appear for that item)

isView : Whether this item is a leaf level item or not

subMenus : if isView is false, then there are multiple menu items under this attribute.

Thus, it is a tree structure that can go till any depth. The items in the menu can change periodically and hence the implementation has to be kept flexible. This data is stored in a plist file and read into an NSArray in the code (already implemented).

I now need to create a slide out menu on the left that will be populated by this data hierarchy. I have created the menu pane and added swipe gestures to it, all of which work fine. The menu has to be a collapsible one where clicking on one menu item expands the subViews below it. If the item clicked on is a view, then a new view is loaded on the rest of the screen with appropriate data.

The problem I am facing is the logic to populate the menu (which is a UITableView ) from the NSArray data. The following are the two approaches I came up with.

  1. Create a UITableView with as many sections as there are items at the top level of the menu. Then iterate through the menu items recursively. For each menu item that is not a view (ie it has subMenus ), create a new section with number of rows equal to the number of subMenus under it. When I come across a menu item that is a view and has no further subMenus , add it as a row to the subsection created for the menu one level above it.

  2. Create a menu with one section and as many rows as the number of menu items at the top level. When a menu item is clicked, insert rows under it to represent its sub-menus. When another menu item on the same level is clicked, collapse the previously expanded menu by deleting the inserted rows. When a menu item with no sub menus is clicked, the rest of the screen is populated with data.

I have tried both the approaches and not been able to go beyond the initial steps. For the first method, I understand that I need to add a UITableView as a part of UITableViewCells , which is good, but I need to do that recursively. For the second approach, I need to know the indexPath of each item clicked which can go to many levels.

I would like some suggestions here about which approach I should take and some guidance over how to go about it. Also, if there is any better way to do this, kindly advice. Thanks.

In my opinion, using UINavigationController is the easiest way. You can push as many UITableViews as you want. If it doesn't fit your design requirement, you can try expandable UITableViews. There are few open sources:

  1. JKExpandTableView
  2. SDNestedTable
  3. iOS-Tree-Component

Thanks for the responses. I ended up doing this using the following control:

Accordion for iOS

It has served my purpose beautifully and I posted it here so that someone with the same requirement may find it.

Take a look at TLIndexPathTools . It has a "Tree" extension that can do this. Try running the Outline sample project . The main task in adapting the sample project would be to write a recursive function to convert your array of nodes into an array of TLIndexPathTreeItem objects. All of the code in the controller:willChangeNode: method is examples of lazy loading and it doesn't sound like you'd need any of that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM