2009-07-29 6 views
0

J'ai une vue générale et un bouton. Je veux que le bouton supprime la ligne sélectionnée dans la vue générale. J'ai lié les chemins d'index de sélection de la vue hiérarchique au contrôleur d'arborescence, connecté le bouton au sélecteur remove: du contrôleur d'arborescence et lié le bouton à la propriété canRemove: du contrôleur d'arborescence. Mais une fois que j'ai fait tout ça (ce que je pensais faire), ça casse complètement la vue générale, quand je dis des pauses je veux dire que ça ne marche plus, ça ne montre aucune donnée que j'ai ajoutée.Suppression d'une ligne d'une vue d'ensemble

Maintenant, pour plus d'informations, voici quelques photos des connexions du contrôleur RAID et du contrôleur d'arborescence.

NSArrayController Connexions

alt text http://snapplr.com/snap/b5a1

NSTreeController Connexions (Comme vous pouvez le voir sans les chemins Index de sélection Binding)

alt text http://snapplr.com/snap/qv62

Une dernière chose, la vue Outline a une source de données qui lui permet de faire glisser et déposer, je pense que a quelque chose à voir avec cela ne fonctionne pas, donc ci-dessous est le code dans la source de données.

Le fichier de mise en œuvre.

#import "DragController.h" 


@implementation DragController 
- (void)awakeFromNib { 

    dragType = [NSArray arrayWithObjects: @"factorialDragType", nil]; 

    [ dragType retain ]; 

    [ treeTable registerForDraggedTypes:dragType ]; 
    NSSortDescriptor* sortDesc = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES]; 
    [groupTreeControl setSortDescriptors:[NSArray arrayWithObject: sortDesc]]; 
    [ sortDesc release ]; 
} 


//------------------------------------ 
#pragma mark NSOutlineView datasource methods -- see NSOutlineViewDataSource 
//--------------------------------------------------------------------------- 
- (BOOL) outlineView : (NSOutlineView *) outlineView 
      writeItems : (NSArray*) items 
     toPasteboard : (NSPasteboard*) pboard { 

    [ pboard declareTypes:dragType owner:self ];   
    // items is an array of _NSArrayControllerTreeNode see http://theocacao.com/document.page/130 for more info 
    draggedNode = [ items objectAtIndex:0 ]; 

    return YES; 
} 




- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(int)index { 

    _NSArrayControllerTreeNode* parentNode = item; 
    _NSArrayControllerTreeNode* siblingNode; 
    _NSControllerTreeProxy* proxy = [ groupTreeControl arrangedObjects ]; 

    NSManagedObject* draggedGroup = [ draggedNode observedObject ]; 

    BOOL draggingDown = NO; 
    BOOL isRootLevelDrag = NO; 

    // ---------------------- 
    // Setup comparison paths 
    // ------------------------- 
    NSIndexPath* draggedPath = [ draggedNode indexPath ]; 
    NSIndexPath* siblingPath = [ NSIndexPath indexPathWithIndex: index ]; 
    if (parentNode == NULL) {  
     isRootLevelDrag = YES; 
    } else { 
     // A non-root drag - the index value is relative to this parent's children 
     siblingPath = [ [ parentNode indexPath ] indexPathByAddingIndex: index ]; 
    } 

    // ---------------------- 
    // Compare paths - modify sibling path for down drags, exit for redundant drags 
    // -----------------------------------------------------------------------------  
    switch ([ draggedPath compare:siblingPath]) { 
     case NSOrderedAscending: // reset path for down dragging 
      if (isRootLevelDrag) { 
       siblingPath = [ NSIndexPath indexPathWithIndex: index - 1];        
      } else { 
       siblingPath = [ [ parentNode indexPath ] indexPathByAddingIndex: index - 1 ]; 
      } 
      draggingDown = YES; 
      break; 

     case NSOrderedSame: 
      return NO; 
      break;    
    } 

    siblingNode = [ proxy nodeAtIndexPath:siblingPath ];  

    // NSLog(@"returning early"); 
    // return NO; // TODO robustify 


    // ------------------------------------------------------------ 
    // SPECIAL CASE: Dragging to the bottom 
    // ------------------------------------------------------------ 
    // - K        - K       - C        - C 
    // - - U        - - C  OR  - U        - F 
    // - - C  ====>  - - F     - F        - K 
    // - - F    - U    - K        - U 
    // ------------------------------------------------------------ 
    if (isRootLevelDrag && siblingNode == NULL) {   
     draggingDown = YES; 
     siblingPath = [ NSIndexPath indexPathWithIndex: [ proxy count ] - 1 ];   
     siblingNode = [ proxy nodeAtIndexPath:siblingPath ] ; 
    } 

    // ------------------------------------------------------------ 
    // Give the dragged item a position relative to it's new sibling 
    // ------------------------------------------------------------ 
    NSManagedObject* sibling = [ siblingNode observedObject ]; 
    NSNumber* bystanderPosition = [ sibling valueForKey:@"position"]; 
    int newPos = (draggingDown ? [ bystanderPosition intValue ] + 1 : [ bystanderPosition intValue ] - 1); 
    [draggedGroup setValue:[ NSNumber numberWithInt:newPos ] forKey:@"position"]; 

    // ---------------------------------------------------------------------------------------------- 
    // Set the new parent for the dragged item, resort the position attributes and refresh the tree 
    // ----------------------------------------------------------------------------------------------  
    [ draggedGroup setValue:[ parentNode observedObject ] forKey:@"parent" ]; 
    [ self resortGroups:[draggedGroup managedObjectContext] forParent:[ parentNode observedObject ] ];   
    [ groupTreeControl rearrangeObjects ]; 
    return YES;    
} 






- (NSArray*) getSubGroups:(NSManagedObjectContext*)objectContext forParent:(NSManagedObject*)parent { 
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"projects" inManagedObjectContext:objectContext]; 

    [request setEntity:entity]; 
    NSSortDescriptor* aSortDesc = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES]; 
    [request setSortDescriptors:[NSArray arrayWithObject: aSortDesc] ]; 
    [aSortDesc release]; 

    NSPredicate* validationPredicate = [NSPredicate predicateWithFormat:@"parent == %@", parent ]; 

    [ request setPredicate:validationPredicate ]; 

    NSError *error = nil; // TODO - check the error bozo 
    return [objectContext executeFetchRequest:request error:&error];  
} 




- (void) resortGroups:(NSManagedObjectContext*)objectContext forParent:(NSManagedObject*)parent { 

    NSArray *array = [ self getSubGroups:objectContext forParent:parent ]; 

    // Reset the indexes... 
    NSEnumerator *enumerator = [array objectEnumerator]; 
    NSManagedObject* anObject; 
    int index = 0; 
    while (anObject = [enumerator nextObject]) { 
     // Multiply index by 10 to make dragging code easier to implement ;) .... 
     [anObject setValue:[ NSNumber numberWithInt:(index * INTERVAL) ] forKey:@"position"];  
     index++; 
    } 


} 

- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(int)index { 

    _NSArrayControllerTreeNode* newParent = item; 

    // drags to the root are always acceptable 
    if (newParent == NULL) { 
     return NSDragOperationGeneric; 
    } 

    // Verify that we are not dragging a parent to one of it's ancestors 
    // causes a parent loop where a group of nodes point to each other and disappear 
    // from the control 
    NSManagedObject* dragged = [ draggedNode observedObject ];  
    NSManagedObject* newP = [ newParent observedObject ]; 

    if ([ self category:dragged isSubCategoryOf:newP ]) { 
     return NO; 
    }  

    return NSDragOperationGeneric; 
} 

- (BOOL) category:(NSManagedObject*)cat isSubCategoryOf:(NSManagedObject*) possibleSub { 

    // Depends on your interpretation of subCategory .... 
    if (cat == possibleSub) { return YES; } 

    NSManagedObject* possSubParent = [possibleSub valueForKey:@"parent"]; 

    if (possSubParent == NULL) { return NO; } 

    while (possSubParent != NULL) {  
     if (possSubParent == cat) { return YES; } 

     // move up the tree 
     possSubParent = [possSubParent valueForKey:@"parent"];   
    } 

    return NO; 
} 




// This method gets called by the framework but the values from bindings are used instead 
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { 
    return NULL; 
} 

/* 
The following are implemented as stubs because they are required when 
implementing an NSOutlineViewDataSource. Because we use bindings on the 
table column these methods are never called. The NSLog statements have been 
included to prove that these methods are not called. 
*/ 
- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { 
    NSLog(@"numberOfChildrenOfItem"); 
    return 1; 
} 

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item { 
    NSLog(@"isItemExpandable"); 
    return NO; 
} 

- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item { 
    NSLog(@"child of Item"); 
    return NULL; 
} 



@end 

Répondre

1

Je l'ai compris, donc je vais poster la réponse pour aider tous ceux qui peuvent avoir le même problème. Pour le réparer, faites-le simplement.

  1. Liez le managedObjectContext de NSTreeController directement à la propriété managedObjectContext du AppDelegate, et enlever toute NSArrayController. NSTreeController peut ajouter/supprimer des éléments à partir du managedObjectContext, mais pas à partir des rangéesObjects de NSArrayController, car il est en lecture seule.

  2. Connectez maintenant le bouton d'ajout à la connexion add: du contrôleur d'arborescence.

  3. Enfin, connectez le bouton Supprimer à la connexion remove: du contrôleur d'arborescence.

0

add:

  • Bouton encastré (D ...
  • Bouton encastré (N ...

sont des « Nouveau » et « Supprimer »?

Si c'est le cas, vous avez laissé votre bouton Supprimer connecté à l'action add:. Connectez-le à remove: à la place. Vous devez également utiliser des boutons de dégradé avec des images NSAddTemplate et NSRemoveTemplate au lieu de boutons en retrait avec des étiquettes verbales (voir le panneau Comptes dans les Préférences Système pour un exemple en action, et the HIG pour une description des boutons enfoncés supposé être utilisé pour.)

+0

Non, le D est pour un bouton différent appelé done. – Joshua

Questions connexes