如何创建像UITableView结构的树?

时间:2014-08-27 02:03:28

标签: ios objective-c tree nsmutablearray nsdictionary

我有一个NSMictionary的NSMutableArray,看起来像这样

<Blank BlankName="V6" Type="Line" CurrentType="Crypt"/>
<Blank BlankName="T3" Type="Ion" CurrentType="Crypt"/>
<Blank BlankName="HU" Type="Sia" CurrentType="Crypt"/>
<Blank BlankName="HF" Type="Ion" CurrentType="Crypt"/>
<Blank BlankName="HU5" Type="Sia" CurrentType="Crypt"/>
<Blank BlankName="HU6" Type="Sia" CurrentType="Less"/>
<Blank BlankName="V6" Type="Line" CurrentType="Less"/>
<Blank BlankName="V66" Type="Line" CurrentType="Less"/>

我正在试图找出如何将它放入树状结构中以便在UITableView中显示。

在此结构中将有3种类型的信息

Header (unique) - CurrentType
 - Sub Header (unique) - Type
  - Info (multiple items) - BlankName

所以使用xml我作为一个例子,UITableView看起来像这样。

Crypt
 - Ion
  - T3
  - HF
 - Line
  - V6
 - Sia
  - HU
  - HU5
Less
 - Line
  - V6
  - V66
 - Sia
  - HU6

我真的没有任何我一直在研究的示例代码,因为我真的不知道从哪里开始,例如,如果有一个特定的结构适合存储这种类型的信息。我没有考虑过排序,因为我不清楚它是如何存储的。

在提出这个问题后,我仍在阅读和调查。

2 个答案:

答案 0 :(得分:1)

要创建一个平面数组 - 更改解析代码以创建如下数组:

[ <Dictionary containing Header 1 title, indentation 0>,
  (Dictionary containing Sub-Header 1 title, indentation 1),
  -Dictionary containing Sub-Header-info 1 title, indentation 2-,
  -Dictionary containing Sub-Header-info 1 title, indentation 2-,
  .....
  (Dictionary containing Sub-Header 2 title, indentation 1),
  .....
  <Dictionary containing Header 2 title, indentation 0>,
  .....
  and so on.
]

您可以创建自己的自定义对象类,而不是字典,以保存缩进和标题以及其他内容的值。会更容易使用:) 干杯:)

答案 1 :(得分:0)

最终,在UITableView(在我看来)中表示项目的最简单方法是使用平面数组(如果您有多个部分,则使用平面数组阵列)。

您需要做的是根据您所在的树的级别来增加缩进级别,但您还需要正确的顺序。

有一个看起来像这样的对象可能会有所帮助:

@interface MyObject 

@property NSInteger level;

@property id object;

@end

现在,遍历您的XML。将此对象上的level属性设置为对象在XML中的正确深度,并让object成为指向原始对象的指针。

当您这样做时,将所有这些对象抛出一个数组,并确保您正确地获得订单。

一旦您构建了数组,现在只需将其与表格视图的数据源一起使用。

对于numberOfRows,返回数组的计数。

当您创建表格视图单元格时,请将缩进级别设置为您在容器对象中设置的level属性。

相关问题