以编程方式将NSTreeController绑定到NSOutlineView

4
为了支持除Swift和ObjC之外的其他语言,我需要了解如何为NSOutlineView设置NSTreeController。
我已经能够在代码中创建NSOutlineView,并提供自己的DataSource委托。但是现在我想切换到使用NSTreeController。我很难弄清楚如何设置绑定和其他关系,因为我能找到的所有示例都假设使用Interface Builder进行设置。

几乎IB中的每个属性都有一个对应的属性。绑定可以在代码中设置。哪个属性或绑定是问题? - Willeke
我以前从未使用过绑定,所以我从零开始。希望有人能分享一些代码片段来完成这个任务。现在我快要弄清楚了。 - Thomas Tempelmann
天啊 - 刚刚我发现已经有一个带有有用示例代码的类似问题了:http://stackoverflow.com/q/31827582/43615 - 奇怪的是我之前没找到它。我仍然可以将其作为更详细的答案,或者删除这个问题。 - Thomas Tempelmann
1个回答

4

我想我已经基本了解了。记录一下,这是我必须添加到我的视图控制器的内容,这个视图控制器已经管理了一个NSOutlineView,我自己管理数据源:

创建一个树节点的类:

@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

将以下属性添加到您的视图控制器中:

@property (nonatomic, retain) NSTreeController *treeController;
@property (nonatomic, retain) NSMutableArray *treeContents;

初始化树形控制器,例如从视图控制器的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是基于单元格的,因此我还必须继续实现数据源方法以为单元格提供值,因为我无法找出如何进行绑定:

-(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


1
苹果提供了一些绑定名称的常量:NSContentBindingNSSelectionIndexPathsBindingNSSortDescriptorsBinding等。 - Willeke
在Swift中,NSContentBinding已经移动到NSBindingName.content。所有其他绑定名称也被移动到了适当的位置。 - eonil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接