从coredata获取父子数组 [英] Get parent-child array from coredata
问题描述
我有一个CoreData模型,可以包含无限数量的孩子。我想显示每个对象的列表,缩进为可读性,如下所示
I have a CoreData model that can contain an infinite number of children. And I want to display a list of each object, indented for readability like so
Object
Object first child
first childs children
first child children
Object second child
Object 2
Object also has children
MOre children
Childs
现在我来自PHP背景。在PHP中,我将创建一个简单的数组,它使用一些函数来构建这个列表,但是这对我来说仍然很困难。
Now I come from a PHP background. 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.
我有一个平面数组有这样的项目:
array.name = @Name;
array.children = nil或coredata rows
array.parent = nil或coredata行
I got a flat array which basically has items like this: array.name = @"Name"; array.children = nil or coredata rows array.parent = nil or coredata row
如何遍历此并显示缩进的列表分组如上。
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];
}
}
结果数组:result。包含一个数组,按顺序。与所有的父母 - >孩子。
在我的case,缩进在需要。 (使用上面没有显示的额外参数)
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)
推荐答案
您可以从一个数据模型开始, p>
You start with a data model with one entity that would look something like:
Node{
name:string
parent<<-->Node.children
children<-->>Node.parent
}
所有父
关系为nil的所有 Node
对象的提取。这将是你的顶级对象。要查找下一级对象,您只需查询每个顶级对象的 children
属性。
To use you would do a fetch for all Node
objects whose parent
relationship was nil. 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.
使用UITableviewDelegate的 tableView:indentationLevelForRowAtIndexPath:
方法缩进行。在这种情况下,您将通过获取每个节点对象然后将它的父关系递归地一直递归到顶部并计算步骤来计算每个行的缩进。
You indent rows using the UITableviewDelegate's tableView:indentationLevelForRowAtIndexPath:
method. 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.
但是,在iOS上,不建议使用缩进的表格。如果您定位iPhone,缩进的表格视图就没有用了,因为您没有足够的屏幕区域来查看有用的表格。相反,使用导航控制器来显示每个显示不同级别的数据层次结构的表视图的层次结构。
However, on iOS, indented tableviews are discouraged. 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. Instead, use a navigation controller to display a hierarchy of tableviews each displaying a different level of the data hierarchy.
这篇关于从coredata获取父子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!