首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可可NSOutlineView取消先前选择

可可NSOutlineView取消先前选择
EN

Stack Overflow用户
提问于 2013-12-11 06:27:58
回答 1查看 608关注 0票数 1

我有一个NSOutlineView,其中有些没有,我想取消选择,或者在对其他单元格进行选择时关闭前面的选择。

我希望我能解释这些屏幕截图。

这里我们有两个父文件夹,文件夹1和文件夹2。我想取消选择文件夹1关于选择文件夹2的暴露三角形。

就像这样,使用

代码语言:javascript
复制
-(BOOL)outlineView:(NSOutlineView *)outlineView shouldExpandItem:(id)item;
-(BOOL)outlineView:(NSOutlineView *)outlineView shouldCollapseItem:(id)item;

我还没找到出路。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-11 08:22:39

实现此委托并使用类似的方法:

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

这里,parent是顶级项数组,在您的示例Folder1和Folder2中是这样的。

编辑:

应用程序.h

代码语言:javascript
复制
@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

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

@end

应用程序.m

代码语言:javascript
复制
@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。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20512058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档