8

を表すモデルオブジェクトが与えられNSTreeControllerた場合、ツリー内でそれらのインデックスパスをどのように見つけ、その後それらを選択しますか?これはやみくもに明らかな問題のようですが、私はそれへの参照を見つけることができないようです。何か案は?

4

2 に答える 2

19

「簡単な」方法はありません。ツリーノードをたどって、次のような一致するインデックスパスを見つける必要があります。

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    

迅速:

拡大

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
    }
}
于 2012-01-29T02:43:59.517 に答える