2011-07-22 53 views
1

我有一個可以包含無限數量子項的CoreData模型。我想要顯示的每個對象的列表,縮進了可讀性,像這樣從coredata獲取父子數組

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

現在我來自一個PHP的背景。在PHP中,我會創建一個簡單的數組,它會遍歷一些函數來構建這個列表,但這對我來說似乎仍然很愚蠢。

我得到一個平面陣列,基本上有這樣的項目: array.name = @「Name」; array.children =無或coredata行 array.parent =無或coredata行

如何遍歷此並顯示列表縮進和分組像上面。

感謝任何指針或例子

着 - 下面指針完成了它:生成的代碼如下:

生成的代碼是(類似下面,我有我自己的調整,但多數民衆贊成細節)

- (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]; 
    } 
} 

生成的數組:結果。包含一個數組,按順序。與所有父母 - >孩子。 在我的情況下,在需要時縮進。 (使用額外的參數不是如上圖所示)

回答

0

你開始與一個實體數據模型,它看起來像:

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

要使用你會做一個獲取所有Node對象,它們的parent關係是零。那將是您的頂級對象。要查找下一級別的對象,您只需查詢每個頂級對象的children屬性。

使用UITableviewDelegate的tableView:indentationLevelForRowAtIndexPath:方法縮進行。在這種情況下,您可以通過獲取每個節點對象,然後將其父節點關係遞歸地移至頂部並計算步驟來計算每行的縮進量。

但是,在iOS上,不鼓勵縮進表格視圖。如果您的目標是iPhone,縮進的tableview接近無用,因爲您沒有足夠的屏幕區域來查看有用的表格數量。相反,使用導航控制器來顯示每個顯示不同級別數據層次結構的表視圖層次結構。