OutlineView dataSource & CoreData

Bonjour,



J'ai une outlineView à  peupler par une dataSource. Mon modèle de données est un dataModel coreData.

L'outlineView est sensée afficher simplement les noms de "Niveau", et on doit pouvoir "expander" afin de voir les noms des "Classe" du niveau (niveau a une relationship to-many lesClasses)



J'ai donc tenté le code suivant :


<br />
- ([color=#743fa4]NSArray[/color] *)sortedClassesOfLevel:([color=#743fa4]NSManagedObject[/color] *)level {<br />
[color=#743fa4]NSSet[/color] *lesClasses=[level [color=#40207c]valueForKey[/color]:[color=#d62c24]@&quot;lesClasses&quot;[/color]];[color=#743fa4]<br />
NSSortDescriptor[color=#000000] *sort=[[[/color]NSSortDescriptor[color=#000000] [/color][color=#40207c]alloc[/color][color=#000000]] [/color][color=#40207c]initWithKey[/color][color=#000000]:[/color][color=#d62c24]@&quot;nom&quot;[/color][/color]<br />
  [color=#40207c]ascending[/color]:[color=#c02d9d]YES[/color]];[color=#40207c]<br />
[color=#743fa4]NSArray[/color][color=#000000] *sortedClasses=[lesClasses [/color]sortedArrayUsingDescriptors[color=#000000]:[[/color][color=#743fa4]NSArray[/color][color=#000000] [/color]arrayWithObject[color=#000000]:sort]];[/color][/color]<br />
[sort [color=#40207c]release[/color]];<br />
[color=#c02d9d]return[/color] sortedClasses;<br />
}<br />
- ([color=#743fa4]NSArray[/color] *)lesNiveaux {[color=#743fa4]<br />
NSFetchRequest[color=#000000] *req=[[[[/color]NSFetchRequest[color=#000000] [/color][color=#40207c]alloc[/color][color=#000000]] [/color][color=#40207c]init[/color][color=#000000]] [/color][color=#40207c]autorelease[/color][color=#000000]];[/color][/color][color=#40207c]<br />
[color=#000000][req [/color]setEntity[color=#000000]:[[/color][color=#743fa4]NSEntityDescription[/color][color=#000000] [/color]entityForName[color=#000000]:[/color][color=#d62c24]@&quot;Niveau&quot;[/color][/color][color=#40207c]<br />
[color=#000000]  [/color]inManagedObjectContext[color=#000000]:[/color][color=#4b8186]managedObjectContext[/color][color=#000000]]];[/color][/color]<br />
[color=#743fa4]NSArray[/color] *lesNiveaux=[[color=#4b8186]managedObjectContext[/color] [color=#40207c]executeFetchRequest[/color]:req [color=#40207c]error[/color]:[color=#c02d9d]nil[/color]];<br />
[color=#c02d9d]return[/color] lesNiveaux;<br />
}<br />
- ([color=#743fa4]NSInteger[/color])outlineView:([color=#743fa4]NSOutlineView[/color] *)outlineView numberOfChildrenOfItem:([color=#c02d9d]id[/color])item {[color=#008324]<br />
//Si c&#39;est le root, le nombre d&#39;enfants est le nombre de niveaux[/color]<br />
[color=#c02d9d]if[/color] (item==[color=#c02d9d]nil[/color]) {<br />
[color=#743fa4]NSArray[/color] *lesNiveaux=[[color=#c02d9d]self[/color] [color=#2e595d]lesNiveaux[/color]];<br />
[color=#c02d9d]return[/color] [lesNiveaux [color=#40207c]count[/color]];<br />
}[color=#008324]<br />
//Si c&#39;est un niveau, il faut trouver combien il y a de classes dans ce niveau[/color]<br />
[color=#c02d9d]if[/color] ([[item [color=#40207c]entity[/color]] [color=#40207c]name[/color]]==[color=#d62c24]@&quot;Niveau&quot;[/color]) {<br />
[color=#743fa4]NSArray[/color] *lesClasses=[[color=#c02d9d]self[/color] [color=#2e595d]sortedClassesOfLevel[/color]:item];<br />
[color=#c02d9d]return[/color] [lesClasses [color=#40207c]count[/color]];<br />
}[color=#c02d9d]<br />
return[color=#000000] [/color][color=#2f2fd0]0[/color][color=#000000];[/color][/color]<br />
}<br />
- ([color=#c02d9d]BOOL[/color])outlineView:([color=#743fa4]NSOutlineView[/color] *)outlineView isItemExpandable:([color=#c02d9d]id[/color])item {[color=#008324]<br />
//Si c&#39;est un niveau, il est expandable, sinon non[/color]<br />
[color=#c02d9d]if[/color] (item==[color=#c02d9d]nil[/color]) [color=#c02d9d]return[/color] [color=#c02d9d]NO[/color];<br />
[color=#c02d9d]return[/color] [[item [color=#40207c]entity[/color]] [color=#40207c]name[/color]]==[color=#d62c24]@&quot;Niveau&quot;[/color] ? [color=#c02d9d]YES[/color] : [color=#c02d9d]NO[/color];<br />
}<br />
- ([color=#c02d9d]id[/color])outlineView:([color=#743fa4]NSOutlineView[/color] *)outlineView child:([color=#743fa4]NSInteger[/color])index ofItem:([color=#c02d9d]id[/color])item {[color=#008324]<br />
//Si c&#39;est le root, il faut les niveaux tries par ordre alphabetique (a voir apres pour faire mieux)[/color]<br />
[color=#743fa4]NSArray[/color] *lesNiveaux=[[color=#c02d9d]self[/color] [color=#2e595d]lesNiveaux[/color]];<br />
[color=#c02d9d]if[/color] (item==[color=#c02d9d]nil[/color]) {<br />
[color=#c02d9d]return[/color] [lesNiveaux [color=#40207c]objectAtIndex[/color]:index];<br />
}[color=#008324]<br />
//Si c&#39;est un niveau on cherche les classes tries par ordre aplphabetique[/color]<br />
[color=#c02d9d]if[/color] ([item [color=#40207c]class[/color]]==[[color=#743fa4]NSManagedObject[/color] [color=#40207c]class[/color]]) {[color=#008324]<br />
//Si c&#39;est un niveau on cherche les classes tries par ordre aplphabetique[/color]<br />
[color=#c02d9d]if[/color] ([[item entity ][color=#40207c]name[/color]]==[color=#d62c24]@&quot;Niveau&quot;[/color]) {<br />
[color=#743fa4]NSArray[/color] *lesClasses=[[color=#c02d9d]self[/color] [color=#2e595d]sortedClassesOfLevel[/color]:item];<br />
[color=#c02d9d]return[/color] [lesClasses [color=#40207c]objectAtIndex[/color]:index];<br />
}<br />
}[color=#008324]<br />
//Normalement, on n&#39;arrive jamais ici ...[/color][color=#c02d9d]<br />
return[color=#000000] [/color]nil[color=#000000];[/color][/color]<br />
}<br />
- ([color=#c02d9d]id[/color])outlineView:([color=#743fa4]NSOutlineView[/color] *)outlineView objectValueForTableColumn:([color=#743fa4]NSTableColumn[/color] *)tableColumn byItem:([color=#c02d9d]id[/color])item {<br />
[color=#c02d9d]return[/color] item==[color=#c02d9d]nil[/color] ? [color=#d62c24]@&quot;&quot;[/color] : [item [color=#40207c]valueForKey[/color]:[color=#d62c24]@&quot;nom&quot;[/color]];<br />
}<br />




Mais cela bricole, je n'ai donc pas la bonne technique. Quelqu'un peut-il me dire ce qui ne va pas ?..

Réponses

  • Bon, en fait, j'ai refais différemment pour coller à  la doc : lors de l'appel de la méthode outlineView:numberOfChildrenOfItem:, je construis une arborescence avec des NSArray et NSDictionary. Chaque dico contient une clef "isLeaf", "children", "value", "nbOfChildren" et "object". Du fait, la dataSOurce maintient elle-même cette arborescence et je n'ai pas de soucis.



    Je ne comprends pas pourquoi je dois refaire une arborescence alors que mon dataModel l'a déjà  image/crazy.gif' class='bbc_emoticon' alt=' B) ' /> . Je seul soucis est peut-être la performance vu que je reconstruis l'arborescence à  chaque reloadData. (il n'y a pas non plus 600 niveaux contenant chacuns 3000 classes...)
    <br />
    <br />
    - ([color=#743fa4]NSInteger[/color])outlineView:([color=#743fa4]NSOutlineView[/color] *)outlineView numberOfChildrenOfItem:([color=#c02d9d]id[/color])item {[color=#008324]<br />
    //Si c&#39;est le root, on recree l&#39;arborescence[/color]<br />
    [color=#c02d9d]if[/color] (item==[color=#c02d9d]nil[/color]) {[color=#4b8186]<br />
    [color=#000000][[/color]lesItemsNiveaux[color=#000000] [/color][color=#40207c]release[/color][color=#000000]];[/color][/color][color=#4b8186]<br />
    lesItemsNiveaux[color=#000000]=[[[/color][color=#743fa4]NSMutableArray[/color][color=#000000] [/color][color=#40207c]array[/color][color=#000000]] [/color][color=#40207c]retain[/color][color=#000000]];[/color][/color][color=#743fa4]<br />
    NSFetchRequest[color=#000000] *req=[[[/color]NSFetchRequest[color=#000000] [/color][color=#40207c]alloc[/color][color=#000000]] [/color][color=#40207c]init[/color][color=#000000]];[/color][/color][color=#40207c]<br />
    [color=#000000][req [/color]setEntity[color=#000000]:[[/color][color=#743fa4]NSEntityDescription[/color][color=#000000] [/color]entityForName[color=#000000]:[/color][color=#d62c24]@&quot;Niveau&quot;[/color][/color][color=#40207c]<br />
    [color=#000000]  [/color]inManagedObjectContext[color=#000000]:[/color][color=#4b8186]managedObjectContext[/color][color=#000000]]];[/color][/color][color=#743fa4]<br />
    NSSortDescriptor[color=#000000] *sort=[[[/color]NSSortDescriptor[color=#000000] [/color][color=#40207c]alloc[/color][color=#000000]] [/color][color=#40207c]initWithKey[/color][color=#000000]:[/color][color=#d62c24]@&quot;nom&quot;[/color][/color]<br />
      [color=#40207c]ascending[/color]:[color=#c02d9d]YES[/color]];[color=#40207c]<br />
    [color=#000000][req [/color]setSortDescriptors[color=#000000]:[[/color][color=#743fa4]NSArray[/color][color=#000000] [/color]arrayWithObject[color=#000000]:sort]];[/color][/color][color=#008324]<br />
    //[sort release];[/color]<br />
    [color=#743fa4]NSError[/color] *error;<br />
    [color=#743fa4]NSArray[/color] *lesNiveauxCoreData=[[color=#4b8186]managedObjectContext[/color] [color=#40207c]executeFetchRequest[/color]:req [color=#40207c]error[/color]:&amp;error];[color=#008324]<br />
    //On cree l&#39;arborescence[/color]<br />
    [color=#c02d9d]for[/color] ([color=#743fa4]NSManagedObject[/color] *unNiveau [color=#c02d9d]in[/color] lesNiveauxCoreData) {[color=#40207c]<br />
    [color=#743fa4]NSArray[/color][color=#000000] *lesClasses=[[unNiveau [/color]valueForKey[color=#000000]:[/color][color=#d62c24]@&quot;lesClasses&quot;[/color][color=#000000]] [/color]sortedArrayUsingDescriptors[color=#000000]:[/color][/color]<br />
    [[color=#743fa4]NSArray[/color] [color=#40207c]arrayWithObject[/color]:sort]];[color=#743fa4]<br />
    NSMutableArray[color=#000000] *lesItemsClasses=[[/color]NSMutableArray[color=#000000] [/color][color=#40207c]array[/color][color=#000000]];[/color][/color]<br />
    [color=#c02d9d]for[/color] ([color=#743fa4]NSManagedObject[/color] *uneClasse [color=#c02d9d]in[/color] lesClasses) {[color=#40207c]<br />
    [color=#743fa4]NSDictionary[/color][color=#000000] *unItemClasse=[[/color][color=#743fa4]NSDictionary[/color][color=#000000] [/color]dictionaryWithObjectsAndKeys[color=#000000]:[/color][/color]<br />
    [[color=#743fa4]NSNumber[/color] [color=#40207c]numberWithInt[/color]:[color=#2f2fd0]0[/color]],[color=#d62c24]@&quot;nbOfChildren&quot;[/color],<br />
    uneClasse, [color=#d62c24]@&quot;object&quot;[/color],<br />
    [[color=#743fa4]NSNumber[/color] [color=#40207c]numberWithBool[/color]:[color=#c02d9d]YES[/color]], [color=#d62c24]@&quot;isLeaf&quot;[/color],<br />
    [uneClasse [color=#40207c]valueForKey[/color]:[color=#d62c24]@&quot;nom&quot;[/color]], [color=#d62c24]@&quot;value&quot;[/color],[color=#c02d9d]nil[/color]];<br />
    [lesItemsClasses [color=#40207c]addObject[/color]:unItemClasse];<br />
    <br />
    }[color=#40207c]<br />
    [color=#743fa4]NSDictionary[/color][color=#000000] *itemNiveau=[[/color][color=#743fa4]NSDictionary[/color][color=#000000] [/color]dictionaryWithObjectsAndKeys[color=#000000]:[/color][/color]<br />
      unNiveau, [color=#d62c24]@&quot;object&quot;[/color],<br />
      [[color=#743fa4]NSNumber[/color] [color=#40207c]numberWithInt[/color]:[lesItemsClasses [color=#40207c]count[/color]]],[color=#d62c24]@&quot;nbOfChildren&quot;[/color],<br />
      [[color=#743fa4]NSNumber[/color] [color=#40207c]numberWithBool[/color]:[color=#c02d9d]NO[/color]], [color=#d62c24]@&quot;isLeaf&quot;[/color],<br />
      [unNiveau [color=#40207c]valueForKey[/color]:[color=#d62c24]@&quot;nom&quot;[/color]],[color=#d62c24]@&quot;value&quot;[/color],<br />
      [[color=#743fa4]NSArray[/color] [color=#40207c]arrayWithArray[/color]:lesItemsClasses], [color=#d62c24]@&quot;children&quot;[/color],[color=#c02d9d]nil[/color]];<br />
    [[color=#4b8186]lesItemsNiveaux[/color] [color=#40207c]addObject[/color]:itemNiveau];<br />
    }[color=#4b8186]<br />
    [color=#c02d9d]return[/color][color=#000000] [[/color]lesItemsNiveaux[color=#000000] [/color][color=#40207c]count[/color][color=#000000]];[/color][/color]<br />
    <br />
    }<br />
    [color=#40207c]NSLog[/color]([color=#d62c24]@&quot;%@&quot;[/color],item);[color=#40207c]<br />
    [color=#c02d9d]return[/color][color=#000000] [[item [/color]objectForKey[color=#000000]:[/color][color=#d62c24]@&quot;nbOfChildren&quot;[/color][color=#000000]] [/color]intValue[color=#000000]];[/color][/color]<br />
    }<br />
    - ([color=#c02d9d]BOOL[/color])outlineView:([color=#743fa4]NSOutlineView[/color] *)outlineView isItemExpandable:([color=#c02d9d]id[/color])item {<br />
    [color=#c02d9d]if[/color] (item==[color=#c02d9d]nil[/color]) [color=#c02d9d]return[/color] [color=#c02d9d]NO[/color];[color=#40207c]<br />
    [color=#c02d9d]return[/color][color=#000000] &#33;[[item [/color]objectForKey[color=#000000]:[/color][color=#d62c24]@&quot;isLeaf&quot;[/color][color=#000000]] [/color]boolValue[color=#000000]];[/color][/color]<br />
    }<br />
    - ([color=#c02d9d]id[/color])outlineView:([color=#743fa4]NSOutlineView[/color] *)outlineView child:([color=#743fa4]NSInteger[/color])index ofItem:([color=#c02d9d]id[/color])item {<br />
    [color=#c02d9d]if[/color] (item==[color=#c02d9d]nil[/color]) {<br />
    [color=#743fa4]NSDictionary[/color] *lItem=[[color=#4b8186]lesItemsNiveaux[/color] [color=#40207c]objectAtIndex[/color]:index];<br />
    [color=#40207c]NSLog[/color]([color=#d62c24]@&quot;%@&quot;[/color],lItem);[color=#4b8186]<br />
    [color=#c02d9d]return[/color][color=#000000] [[/color]lesItemsNiveaux[color=#000000] [/color][color=#40207c]objectAtIndex[/color][color=#000000]:index];[/color][/color]<br />
    }[color=#40207c]<br />
    [color=#c02d9d]if[/color][color=#000000] (&#33;[[item [/color]objectForKey[color=#000000]:[/color][color=#d62c24]@&quot;isLeaf&quot;[/color][color=#000000]][/color]boolValue[color=#000000]]) {[/color][/color]<br />
    [color=#743fa4]NSArray[/color] *children=[item [color=#40207c]objectForKey[/color]:[color=#d62c24]@&quot;children&quot;[/color]];<br />
    [color=#40207c]NSLog[/color]([color=#d62c24]@&quot;%@&quot;[/color],children);[color=#40207c]<br />
    [color=#c02d9d]return[/color][color=#000000] [[item [/color]objectForKey[color=#000000]:[/color][color=#d62c24]@&quot;children&quot;[/color][color=#000000]] [/color]objectAtIndex[color=#000000]:index];[/color][/color]<br />
    }[color=#008324]<br />
    //Ceci n&#39;est pas sensé se produire, mais bon...[/color][color=#c02d9d]<br />
    return[color=#000000] [/color]nil[color=#000000];[/color][/color]<br />
    <br />
    }<br />
    - ([color=#c02d9d]id[/color])outlineView:([color=#743fa4]NSOutlineView[/color] *)outlineView objectValueForTableColumn:([color=#743fa4]NSTableColumn[/color] *)tableColumn byItem:([color=#c02d9d]id[/color])item {<br />
    [color=#40207c]NSLog[/color]([color=#d62c24]@&quot;%@&quot;[/color],item);<br />
    [color=#c02d9d]return[/color] item==[color=#c02d9d]nil[/color] ? [color=#d62c24]@&quot;&quot;[/color] : [item [color=#40207c]objectForKey[/color]:[color=#d62c24]@&quot;value&quot;[/color]];<br />
    }<br />
    


    Si ça peut servir à  quelqu'un ... (j'aime bien répondre à  mes propres questions !) image/wink.png' class='bbc_emoticon' alt=';)' />
Connectez-vous ou Inscrivez-vous pour répondre.