gpt4 book ai didi

cocoa - 给定模型对象,如何在 NSTreeController 中找到索引路径?

转载 作者:行者123 更新时间:2023-12-03 16:05:55 27 4
gpt4 key购买 nike

给定 NSTreeController 表示的模型对象,如何在树中找到它们的索引路径并随后选择它们?这似乎是一个非常明显的问题,但我似乎找不到任何引用。有什么想法吗?

最佳答案

没有“简单”的方法,您必须遍历树节点并找到匹配的索引路径,例如:

Objective-C:

类别

@implementation NSTreeController (Additions)

- (NSIndexPath*)indexPathOfObject:(id)anObject
{
return [self indexPathOfObject:anObject inNodes:[[self arrangedObjects] childNodes]];
}

- (NSIndexPath*)indexPathOfObject:(id)anObject inNodes:(NSArray*)nodes
{
for(NSTreeNode* node in nodes)
{
if([[node representedObject] isEqual:anObject])
return [node indexPath];
if([[node childNodes] count])
{
NSIndexPath* path = [self indexPathOfObject:anObject inNodes:[node childNodes]];
if(path)
return path;
}
}
return nil;
}
@end

swift :

扩展

extension NSTreeController {

func indexPathOfObject(anObject:NSObject) -> NSIndexPath? {
return self.indexPathOfObject(anObject, nodes: self.arrangedObjects.childNodes)
}

func indexPathOfObject(anObject:NSObject, nodes:[NSTreeNode]!) -> NSIndexPath? {
for node in nodes {
if (anObject == node.representedObject as! NSObject) {
return node.indexPath
}
if (node.childNodes != nil) {
if let path:NSIndexPath = self.indexPathOfObject(anObject, nodes: node.childNodes)
{
return path
}
}
}
return nil
}
}

关于cocoa - 给定模型对象,如何在 NSTreeController 中找到索引路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9050028/

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