給定NSTreeController
表示的模型對象,如何在樹中找到它們的索引路徑並隨後選擇它們?這似乎是一個非常明顯的問題,但我似乎無法找到任何參考。有任何想法嗎?給定模型對象,如何在NSTreeController中查找索引路徑?
8
A
回答
18
有沒有 「簡單」 的方式,你必須走的樹節點,並找到匹配的索引路徑,是這樣的:
的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
}
}
-1
爲什麼不使用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/...
我假設你在這裏尋找一個文件路徑,但你可以調整你的數據源可能代表的任何東西。
+0
問題是尋找樹中的任何給定對象的NSIndexPath,以便您可以通過編程方式將樹控制器的selectedIndexPath(s)更改爲那個。您假定該對象已被選中。如果是這樣,你只需從樹形控制器中獲取selectionIndexPath! – stevesliva 2014-05-20 21:00:44
相關問題
- 1. Javascript - 在嵌套對象中查找對象引用的路徑
- 2. 查找給定路徑的根路徑
- 3. 如何在使用NSTreeController時查找NSOutlineView行索引
- 4. Rails在索引頁上查找單個模型對象
- 5. 在對象內部查找JSON路徑
- 6. 在原型數組中查找對象的索引Javascript
- 7. 遍歷multimap中查找從給定值路徑給定鍵
- 8. Rails鏈接與模型對象路徑
- 9. 在自定義對象列表中查找索引
- 10. 我們如何在表格視圖中找到索引路徑?
- 11. 地圖API:在兩個給定路徑中查找最長的公共路徑
- 12. 如何查找python多列表中給定值的索引?
- 13. 如何查找數組中給定元素的所有索引?
- 14. SSIS項目部署模型:如何查找特定對象(SqlServer2012)
- 15. 如何查找屬於多個模型對象的模型對象
- 16. 檢查對象是否在路徑中
- 17. 對象的方法查找路徑(模塊&方法重寫)
- 18. 檢索所有的NSTreeController的對象
- 19. 如何在intern.js中添加javascript查找目錄(搜索路徑)
- 20. 給定一個FirebaseListObservable對象,如何獲取其路徑?
- 21. 如何查找UIImageView路徑
- 22. 查找絕對路徑
- 23. 在給定路徑找不到文件
- 24. 如何檢查給定路徑是否存在路由(ASP.NET MVC)?
- 25. 查找引用給定對象的限定名稱
- 26. 在nltk.tree.Tree中查找路徑
- 27. 如何在xcode 3.2中找到搜索路徑\標題搜索路徑設置
- 28. PHP路徑中的未定義索引
- 29. 如何僅返回Haystack中特定類型的索引對象
- 30. 給定一個對象列表,我如何找到任何特定對象的索引?
哎喲,那真是低效。我正在考慮編寫一個樹型控制器的子類,以保持模型和treenodes之間的映射。或者可能是模型中的一個類別,它保留對相關treenode的引用。 – Tony 2012-01-29 04:46:07
所有你需要在你的子類中做的事情是維護一個扁平的'NSMutableArray'樹節點。當然,你需要小心,節點的所有修改都會反映在你的數組中。 – 2012-01-29 06:00:03
嗯,我正在考慮一個'NSMutableDictionary'將模型對象或objectID映射到'NSTreeNode's,因爲它對於查找來說似乎更有效率。有沒有什麼原因'NSmutableArray'可能工作betteR? – Tony 2012-01-29 16:56:22