给定模型对象,如何在NSTreeController中找到索引路径? [英] Given model object, how to find index path in NSTreeController?

查看:204
本文介绍了给定模型对象,如何在NSTreeController中找到索引路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定 NSTreeController 的模型对象,如何在树中找到它们的索引路径,然后选择它们?这似乎是一个明显的问题,但我似乎没有找到任何参考。任何想法?

解决方案

没有简单的方式,你必须走树节点并找到一个匹配的索引路径例如:



Objective-C:



类别

  @implementation NSTreeController(附加)

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

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

Swift: / p>

扩展

 扩展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 nil
}
}


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?

解决方案

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

这篇关于给定模型对象,如何在NSTreeController中找到索引路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆