Reputation: 55
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.
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 (i.e. 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.
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.
Upvotes: 2
Views: 2042
Reputation: 55
Thanks for the responses. I ended up doing this using the following control:
It has served my purpose beautifully and I posted it here so that someone with the same requirement may find it.
Upvotes: 1
Reputation: 5298
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:
Upvotes: 3
Reputation: 9915
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.
Upvotes: 0