1

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

我希望我可以用这些屏幕截图来解释。

在此处输入图像描述

这里我们有两个父文件夹,文件夹 1 和文件夹 2。我想在选择文件夹 2 的显示三角形时取消选择文件夹 1。

在此处输入图像描述

像这样的东西,使用

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

我还没有找到出路。

谢谢

4

1 回答 1

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

再加上一种方法写在上面。

并将 NSOutlineView 的委托设置为 IB 中的 AppDelegate。

于 2013-12-11T08:22:39.887 回答