2012-01-29 33 views

Trả lời

18

Không có cách nào "dễ dàng", bạn phải đi bộ các nút cây và tìm thấy một con đường chỉ số phù hợp, cái gì đó như:

Objective-C:

Thể loại

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

mở rộng

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

Ouch, điều đó thực sự không hiệu quả. Tôi đang nghĩ đến việc viết một phân lớp của treecontroller giữ một ánh xạ giữa mô hình và treenodes. Hoặc có thể là một danh mục trên mô hình giữ tham chiếu đến treenode liên quan. – Tony

+0

Tất cả những gì bạn cần làm trong lớp con của bạn là duy trì một 'NSMutableArray' phẳng của các nút cây. Bạn cần phải cẩn thận rằng tất cả các sửa đổi của các nút được phản ánh trong mảng của bạn, tất nhiên. –

+0

Hmm, tôi đã nghĩ đến một ánh xạ 'NSMutableDictionary' hoặc đối tượng mô hình hoặc objectID thành' NSTreeNode's, vì nó có vẻ hiệu quả hơn khi tra cứu. Có bất kỳ lý do 'NSmutableArray' có thể làm việc betteR? – Tony

-1

Tại sao không sử dụng NSOutlineView để có được những mặt hàng mẹ như thế này:

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); 

chí này ra: ./item1/item2/item3/...

Tôi giả sử bạn đang tìm kiếm một đường dẫn tệp ở đây nhưng bạn có thể điều chỉnh cho bất kỳ nguồn dữ liệu nào của bạn có thể đại diện.

+0

Câu hỏi đang tìm kiếm một NSIndexPath cho bất kỳ đối tượng nhất định trong cây, để bạn có thể thay đổi selectedIndexPath (s) của bộ điều khiển cây thành một chương trình đó. Bạn đang giả định rằng đối tượng đã được chọn. Nếu vậy, bạn chỉ cần lấy selectionIndexPath từ bộ điều khiển cây! – stevesliva

Các vấn đề liên quan