2013-12-11 32 views
1

我有一個NSOutlineView,其中一些孩子有一些沒有。我想取消選擇,或者在其他單元格上進行選擇時關閉先前的選擇。Cocoa NSOutlineView取消選擇上一個選項

我希望我能用這些屏幕截圖來解釋。

enter image description here

在這裏,我們有兩個父文件夾,文件夾1和文件夾2我要取消選擇上選擇文件夾2.

的三角形enter image description here

事情是這樣的文件夾1,使用

-(BOOL)outlineView:(NSOutlineView *)outlineView shouldExpandItem:(id)item; 
-(BOOL)outlineView:(NSOutlineView *)outlineView shouldCollapseItem:(id)item; 

我還沒找到出路。

感謝

回答

1

實現這一委託並使用類似的東西:

- (void)outlineViewItemDidExpand:(NSNotification *)notification{ 
    for (id parent in list) { 
     if ([notification.userInfo objectForKey:@"NSObject"] == parent) { 
      continue; 
     } 
     [notification.object collapseItem:parent]; 
    } 
} 

這裏parent是頂級項目陣列,如在你的例子Folder1中和文件夾2。

編輯:

Appdelegate.h

@interface AppDelegate : NSObject <NSApplicationDelegate> 

@property (assign) IBOutlet NSWindow *window; 

@property(strong) NSDictionary *firstParent; 
@property(strong) NSDictionary *secondParent; 
@property(strong) NSArray *list; 

@end 

Appdelegate.m

@implementation AppDelegate 

@synthesize firstParent; 
@synthesize secondParent; 
@synthesize list; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    // Insert code here to initialize your application 
} 

- (id)init { 
    self = [super init]; 
    if (self) { 

     firstParent = [NSDictionary dictionaryWithObjectsAndKeys:@"A",@"parent", 
         [NSArray arrayWithObjects:@"Apple",@"Aeroplane", nil],@"children", nil]; 

     secondParent = [NSDictionary dictionaryWithObjectsAndKeys:@"B",@"parent", 
         [NSArray arrayWithObjects:@"Ball",@"Baloon", nil],@"children", nil]; 
     list = [NSArray arrayWithObjects:firstParent,secondParent, nil]; 

    } 
    return self; 
} 


- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item{ 
    if ([item isKindOfClass:[NSDictionary class]]) { 
     return YES; 
    } 
    else { 
     return NO; 
    } 
} 

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item{ 

    if (item == nil) { //item is nil when the outline view wants to inquire for root level items 
     return [list count]; 
    } 

    if ([item isKindOfClass:[NSDictionary class]]) { 
     return [[item objectForKey:@"children"] count]; 
    } 

    return 0; 
} 

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item{ 

    if (item == nil) { //item is nil when the outline view wants to inquire for root level items 
     return [list objectAtIndex:index]; 
    } 

    if ([item isKindOfClass:[NSDictionary class]]) { 
     return [[item objectForKey:@"children"] objectAtIndex:index]; 
    } 

    return nil; 
} 

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)theColumn byItem:(id)item 
{ 

    if ([[theColumn identifier] isEqualToString:@"children"]) { 
     if ([item isKindOfClass:[NSDictionary class]]) { 
      return [NSString stringWithFormat:@"%ld kids",[[item objectForKey:@"children"] count]]; 
     } 
     return item; 
    } 
    else{ 
     if ([item isKindOfClass:[NSDictionary class]]) { 
      return [item objectForKey:@"parent"]; 
     } 
    } 

    return nil; 
} 

隨着多一個寫在頂部方法。

然後在IB中將NSOutlineView的代理設置爲AppDelegate。

+0

所有摺疊方法都被成功調用,但實際的公開內容沒有被取消選擇。我的意思是說,父母不會被關閉。 – Xander

+0

讓我發佈我的完整代碼.... –

+0

是的..將等待如此 – Xander

相關問題