简体   繁体   English

从 coredata 获取父子数组

[英]Get parent-child array from coredata

I have a CoreData model that can contain an infinite number of children.我有一个 CoreData model 可以包含无限数量的孩子。 And I want to display a list of each object, indented for readability like so我想显示每个 object 的列表,为了便于阅读,像这样缩进

Object
  Object first child
    first childs children
    first child children
  Object second child
Object 2
  Object also has children
  MOre children
    Childs

Now I come from a PHP background.现在我来自 PHP 背景。 and in PHP I would create a simple array which Ill traverse with some functions to build this list but this still seems stupidly hard for me.在 PHP 中,我将创建一个简单的数组,我将使用一些函数遍历该数组来构建此列表,但这对我来说似乎仍然非常困难。

I got a flat array which basically has items like this: array.name = @"Name";我有一个平面数组,它基本上有这样的项目:array.name = @"Name"; array.children = nil or coredata rows array.parent = nil or coredata row array.children = nil 或 coredata 行 array.parent = nil 或 coredata 行

How do I traverse this and display a list indented and grouped like above.我如何遍历它并显示一个像上面一样缩进和分组的列表。

Thanks in forward for any pointers or examples感谢您提供任何指示或示例

-- Finished it with pointers below: resulting code as follows: -- 用下面的指针完成它:结果代码如下:

The resulting code is (similar to the following, I have my own adjustments but thats specifics)生成的代码是(类似于以下,我有自己的调整,但多数民众赞成在细节)

- (NSArray *)flattenGroupsWithParent:(NSManagedObject<ECCGroup> *)parent {
    //findGroupsForGroups gets all nodes with parent: parent.
    NSArray *children = [dataSource findGroupsForGroup:parent];
    for (NSManagedObject<ECCGroup> *child in children) {
        ECCGroupNode *node = [[ECCGroupNode alloc] initWithGroup:child label:child.name];
        [result addObject:node];
        [result addObjectsFromArray:[self flattenGroupsWithParent:child]];
        [node release];
    }
}

The resulting array: result.结果数组:结果。 contains an array, in order.包含一个数组,按顺序排列。 with all parents -> children.与所有父母-> 孩子。 In my case, indented where required.就我而言,在需要的地方缩进。 (using extra parameters not shown above) (使用上面未显示的额外参数)

You start with a data model with one entity that would look something like:您从数据 model 开始,其中一个实体看起来像:

Node{
  name:string
  parent<<-->Node.children
  children<-->>Node.parent
}

To use you would do a fetch for all Node objects whose parent relationship was nil.要使用,您将对所有parent关系为 nil 的Node对象进行获取。 That would be your top level objects.那将是您的顶级对象。 To find the next level of objects you would just query each top level object's children attribute.要查找下一级对象,您只需查询每个顶级对象的children属性。

You indent rows using the UITableviewDelegate's tableView:indentationLevelForRowAtIndexPath: method.您使用 UITableviewDelegate 的tableView:indentationLevelForRowAtIndexPath:方法缩进行。 In this case you would calculate the indent for each row by taking each node object and then walking it's parent relationship recursively all the way to the top and counting the steps.在这种情况下,您将通过获取每个节点 object 然后递归地将其父关系一直走到顶部并计算步骤来计算每一行的缩进。

However, on iOS, indented tableviews are discouraged.但是,在 iOS 上,不鼓励缩进的 tableviews。 If you are targeting the iPhone, an indented tableview is next to useless because you don't have enough screen area to see a useful amount of the table.如果您的目标是 iPhone,缩进的 tableview 几乎没有用处,因为您没有足够的屏幕区域来查看有用数量的表格。 Instead, use a navigation controller to display a hierarchy of tableviews each displaying a different level of the data hierarchy.相反,使用导航 controller 来显示表格视图的层次结构,每个视图显示不同级别的数据层次结构。

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

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