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

发布于 2024-12-29 10:46:02 字数 90 浏览 0 评论 0原文

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

Given model objects that NSTreeController represents, how do you find their index paths in the tree and subsequently select them? This seems to be a blindingly obvious problem, but I can't seem to find any reference to it. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三寸金莲 2025-01-05 10:46:02

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

Objective-C:

Category

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

There's no "easy" way, you have to walk the tree nodes and find a matching index path, something like:

Objective-C:

Category

@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

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
    }
}
简单 2025-01-05 10:46:02

为什么不使用 NSOutlineView 来获取这样的父项:

NSMutableArray *selectedItemArray = [[NSMutableArray alloc] init];

[selectedItemArray addObject:[self.OutlineView itemAtRow:[self.OutlineView selectedRow]]];

while ([self.OutlineView parentForItem:[selectedItemArray lastObject]]) {
  [selectedItemArray addObject:[self.OutlineView parentForItem:[selectedItemArray lastObject]]];
}

NSString *selectedPath = @".";
while ([selectedItemArray count] > 0) {
  OBJECTtype *singleItem = [selectedItemArray lastObject];
  selectedPath = [selectedPath stringByAppendingString:[NSString stringWithFormat:@"/%@", singleItem.name]];
  selectedItemArray removeLastObject];
}

NSLog(@"Final Path: %@", selectedPath);

这将输出: ./item1/item2/item3/...

我假设您正在此处查找文件路径,但您可以根据数据源进行调整代表。

Why not use the NSOutlineView to get the parent items like this:

NSMutableArray *selectedItemArray = [[NSMutableArray alloc] init];

[selectedItemArray addObject:[self.OutlineView itemAtRow:[self.OutlineView selectedRow]]];

while ([self.OutlineView parentForItem:[selectedItemArray lastObject]]) {
  [selectedItemArray addObject:[self.OutlineView parentForItem:[selectedItemArray lastObject]]];
}

NSString *selectedPath = @".";
while ([selectedItemArray count] > 0) {
  OBJECTtype *singleItem = [selectedItemArray lastObject];
  selectedPath = [selectedPath stringByAppendingString:[NSString stringWithFormat:@"/%@", singleItem.name]];
  selectedItemArray removeLastObject];
}

NSLog(@"Final Path: %@", selectedPath);

This will output: ./item1/item2/item3/...

I'm assuming you are looking for a file path here but you can adjust for whatever your data source may represent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文