简体   繁体   English

如何在iOS中创建分层菜单?

[英]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) MenuText :(该项目将出现的文本)

isView : Whether this item is a leaf level item or not isView :此项目是否为叶级项目

subMenus : if isView is false, then there are multiple menu items under this attribute. subMenus :如果isView为false,则此属性下有多个菜单项。

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). 此数据存储在plist文件中,并通过代码(已实现)读入NSArray

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. 该菜单必须是可折叠的菜单,在该菜单上单击一个菜单项可展开其下方的子subViews 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. 我面临的问题是从NSArray数据填充菜单(这是UITableView )的逻辑。 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. 创建一个UITableView ,其中的节与菜单顶层的项数一样多。 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. 对于不是视图的每个菜单项(即,它具有subMenus ),请创建一个新节,其行数等于其下的subMenus数。 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. 当我遇到一个菜单项,即一个视图且没有其他subMenus ,请将其作为一行添加到为该菜单项上一级菜单创建的子节中。

  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. 对于第一种方法,我知道我需要添加一个UITableView作为的一部分UITableViewCells ,这是很好的,但我需要做的递归。 For the second approach, I need to know the indexPath of each item clicked which can go to many levels. 对于第二种方法,我需要知道单击的每个项目的indexPath都可以进入多个级别。

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. 我认为,使用UINavigationController是最简单的方法。 You can push as many UITableViews as you want. 您可以根据需要推送任意数量的UITableViews。 If it doesn't fit your design requirement, you can try expandable UITableViews. 如果不符合您的设计要求,则可以尝试展开式UITableViews。 There are few open sources: 开源很少:

  1. JKExpandTableView JKExpandTableView
  2. SDNestedTable SDNestedTable
  3. iOS-Tree-Component iOS的树组分

Thanks for the responses. 感谢您的答复。 I ended up doing this using the following control: 我最终使用以下控件进行了此操作:

Accordion for iOS 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 . 看看TLIndexPathTools It has a "Tree" extension that can do this. 它有一个“ Tree”扩展名可以做到这一点。 Try running the Outline sample project . 尝试运行Outline示例项目 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. 适应示例项目的主要任务是编写一个递归函数,以将节点数组转换为TLIndexPathTreeItem对象数组。 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. controller:willChangeNode:方法中的所有代码都是延迟加载的示例,听起来好像不需要任何这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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