gpt4 book ai didi

macos - 以编程方式将 NSTreeController 绑定(bind)到 NSOutlineView

转载 作者:行者123 更新时间:2023-12-03 19:39:06 26 4
gpt4 key购买 nike

为了支持与 Swift 和 ObjC 不同的语言,我需要了解如何设置 NSTreeController对于 NSOutlineView .

我已经能够创建 NSOutlineView在代码中,以及提供我自己的 DataSource 委托(delegate)。但现在我喜欢改用 NSTreeController .我很难弄清楚如何设置绑定(bind)和其他关系,因为我能找到的所有示例都假设使用 Interface Builder 进行设置。

最佳答案

我想我现在已经弄清楚了基础知识。作为记录,这是我必须添加到已经管理 NSOutlineView 的 View Controller 中的内容,我自己在其中管理数据源:

为树节点创建一个类:

@interface DataNode : NSObject {}
@property (retain) NSMutableArray *children;
@property (retain) NSString *firstText; // text for 1st column
@property (retain) NSString *secondText; // text for 2nd column
@end

@implementation DataNode
- (instancetype)init {
self.children = [NSMutableArray array];
return self;
}
- (BOOL) isLeaf {
return self.children.count == 0;
}
@end

将这些属性添加到您的 View Controller :
@property (nonatomic, retain) NSTreeController *treeController;
@property (nonatomic, retain) NSMutableArray *treeContents;

初始化树 Controller ,例如从 View Controller 的 awakeFromNib :
self.treeContents = [NSMutableArray array]; // holds the add nodes

self.treeController = [[NSTreeController alloc] init];
[self.treeController setLeafKeyPath:@"isLeaf"]; // refers to DataNode
[self.treeController setChildrenKeyPath:@"children"]; // refers to DataNode

// set up the bindings
[self.treeController bind:@"contentArray" toObject:self withKeyPath:@"treeContents" options:@{NSRaisesForNotApplicableKeysBindingOption:@YES, NSConditionallySetsEditableBindingOption:@YES}];
[self.table bind:@"content" toObject:self.treeController withKeyPath:@"arrangedObjects" options:@{NSAlwaysPresentsApplicationModalAlertsBindingOption:@YES}];
[self.table bind:@"selectionIndexPaths" toObject:self.treeController withKeyPath:@"selectionIndexPaths" options:@{}];
[self.table bind:@"sortDescriptors" toObject:self.treeController withKeyPath:@"sortDescriptors" options:@{}];

将节点添加到树的根:
DataNode *node = [[DataNode alloc] init];
node.firstText = [NSString stringWithFormat:@"1 - %d", i1];
node.secondText = [NSString stringWithFormat:@"2 - %d", i1];
NSIndexPath *loc = [NSIndexPath indexPathWithIndex:self.contents.count]; // appends to end of list
[self.treeController insertObject:node atArrangedObjectIndexPath:loc];

由于我的 NSOutlineView 是基于单元格的,我还必须继续实现 DataSource 方法以便为单元格提供值,因为我无法弄清楚如何为此进行绑定(bind):
-(NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return 0; // never called (due to using NSTreeController)
}

-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return nil; // never called (due to using NSTreeController)
}

-(BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
return NO; // never called (due to using NSTreeController)
}

-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
DataNode *node = [item representedObject];
return [node valueForKey:tableColumn.identifier];
}
objectValueForTableColumn方法假定表列的标识符已设置为 firstTextsecondText , 分别。

关于macos - 以编程方式将 NSTreeController 绑定(bind)到 NSOutlineView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42788993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com