2009-07-08 38 views

回答

15

當你找不到東西的時候,請記住看超類。在這種情況下,您需要的方法之一來自NSTableView,它是NSOutlineView的直接超類。

的解決方案是get the row index for the item using rowForItem:,如果它不是-1(項目不可見/未找到),create an index set with it with [NSIndexSet indexSetWithIndex:]並傳遞索引設置爲the selectRowIndexes:byExtendingSelection: method

+0

嗨,彼得,謝謝你的回答。我已經知道了方法selectRowIndexes:byExtendingSelection :.問題是NSOutlineView正在使用NSIndexPath而不是NSIndexSet。 – cocoafan 2009-07-08 13:02:13

+0

我在NSOutlineView文檔中看不到NSIndexPath的單個實例。也許你正在考慮NSTreeController,你沒有使用它。此外,大綱視圖*是一個表視圖,這意味着所有的表視圖功能應該在大綱視圖中正常工作。 – 2009-07-08 13:34:36

+0

是的,我明白了。不好的是,沒有內置的解決方案不依賴於我的數據源。我必須在我的數據源中編寫額外的代碼,對吧? – cocoafan 2009-07-08 19:54:54

1

不,不存在selectItem:方法,但有一個rowForItem:方法。如果您將其與上述使用selectRowIndexes:byExtendingSelection:的建議結合起來,您應該擁有所有需要的信息。

如果你真的希望擁有選擇一個項目,我會建議呼籲setSelectedItem:一致性的緣故的方法,你可以寫這樣的事情在一個類別上NSOutlineView

- (void)setSelectedItem:(id)item { 
    NSInteger itemIndex = [self rowForItem:item]; 
    if (itemIndex < 0) { 
     // You need to decide what happens if the item doesn't exist 
     return; 
    } 

    [self selectRowIndexes:[NSIndexSet indexSetWithIndex:itemIndex] byExtendingSelection:NO]; 
} 

我不知道,如果這段代碼實際上有效;我只是爲了說明這個概念而把它弄虛作假。

18

這是我終於結束了。建議和更正總是受歡迎的。

@implementation NSOutlineView (Additions) 

- (void)expandParentsOfItem:(id)item { 
    while (item != nil) { 
     id parent = [self parentForItem: item]; 
     if (![self isExpandable: parent]) 
      break; 
     if (![self isItemExpanded: parent]) 
      [self expandItem: parent]; 
     item = parent; 
    } 
} 

- (void)selectItem:(id)item { 
    NSInteger itemIndex = [self rowForItem:item]; 
    if (itemIndex < 0) { 
     [self expandParentsOfItem: item]; 
     itemIndex = [self rowForItem:item]; 
     if (itemIndex < 0) 
      return; 
    } 

    [self selectRowIndexes: [NSIndexSet indexSetWithIndex: itemIndex] byExtendingSelection: NO]; 
} 
@end 
0

這是我用來以編程方式選擇PXSourceList中的項目的代碼片段。

sourceList是一個普通的PXSouceList對象,我想選擇第一組大綱中的第二項。

NSInteger itemRow = [sourceList rowForItem:[[(SourceListItem *)[sourceListItems objectAtIndex:0] children] objectAtIndex:1]]; 
    [sourceList selectRowIndexes:[NSIndexSet indexSetWithIndex:itemRow] byExtendingSelection:YES]; 

如果你還不知道,PXSourceList是一個NSOutlineView的最佳替代品,如果你正在尋找的iTunes /郵件風格輪廓。撿起來: PxSourceList

相關問題