Cocoa NSOutlineView 取消选择先前的选择

Posted

技术标签:

【中文标题】Cocoa NSOutlineView 取消选择先前的选择【英文标题】:Cocoa NSOutlineView deselect previous selection 【发布时间】:2013-12-11 06:27:58 【问题描述】:

我有一个 NSOutlineView,其中一些有孩子,有些没有。当在其他单元格上进行选择时,我想取消选择或关闭之前的选择。

我希望我能用这些屏幕截图来解释一下。

这里我们有两个父文件夹,文件夹 1 和文件夹 2。我想在选择文件夹 2 的显示三角形时取消选择文件夹 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 和 Folder2。

编辑:

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的delegate为AppDelegate。

【讨论】:

所有折叠方法都被成功调用,但实际披露并没有被取消选择。我的意思是说,父母不会被关闭。

以上是关于Cocoa NSOutlineView 取消选择先前的选择的主要内容,如果未能解决你的问题,请参考以下文章

NSTreeController/NSOutlineView 失去选择

MacOS-NSOutlineView使用

使用 NSOutlineView 作为文件浏览器,从给定目录开始

以编程方式将核心数据实体中的项目放入 NSOutlineView?

initWithFrame 未调用,但 awakeFromNib 是

如何自动选择插入到 NSOutlineView & Core Data 中的项目