2014-03-30 34 views
0

我有NSOutlineView基於視圖的一個列標題+圖像/文本/按鈕NSOutlineView視圖基於 - 顯示按鈕僅當選擇

我想顯示僅當選擇的項目按鈕。

我使用下面的代碼:

- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item { 
    .... 
    SidebarTableCellView *sidebarTableCellView = [outlineView makeViewWithIdentifier:@"MainCell" owner:self]; 
    [sidebarTableCellView.buttonProgramme setHidden:YES]; 
    if (outlineView.selectedRow == [outlineView rowForItem:item]) 
      [sidebarTableCellView.buttonProgramme setHidden:NO]; 

但它不工作,按鈕總是隱藏!我可以'弄清楚是什麼問題?

編輯:斷點if語句=> if語句永遠比不上

回答

0

我設法通過添加以下代碼(在viewForTableColumn沒有變化)來解決我的問題:

- (void)outlineViewSelectionDidChange:(NSNotification *)notification { 

    NSNotificationCenter * center = [NSNotificationCenter defaultCenter]; 
    [center removeObserver:self 
         name:@"NSOutlineViewSelectionDidChangeNotification" 
        object:self.sidebarOutlineView]; 

    NSInteger s = [self.sidebarOutlineView selectedRow]; 
    NSInteger lastSelectedRow = [[self.globalVariableDictionary objectForKey:@"lastSelectedRow"] integerValue]; 
    [self.globalVariableDictionary setObject:[NSNumber numberWithInteger:s] forKey:@"lastSelectedRow"]; 

    [self.sidebarOutlineView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:s] 
             columnIndexes:[NSIndexSet indexSetWithIndex:0]]; 
    [self.sidebarOutlineView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:lastSelectedRow] 
             columnIndexes:[NSIndexSet indexSetWithIndex:0]]; 
    [self.sidebarOutlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:s] byExtendingSelection:NO]; 

    [center addObserver:self 
       selector:@selector(outlineViewSelectionDidChange:) 
        name:@"NSOutlineViewSelectionDidChangeNotification" 
       object:self.sidebarOutlineView]; 

    [center addObserver:self 
       selector:@selector(outlineViewSelectionDidChange:) 
        name:@"NSOutlineViewSelectionDidChangeNotification" 
       object:self.sidebarOutlineView]; 
} 

通過這樣做,我們強制viewForTableColumn的調用:當用戶更改選擇。

謝謝!

相關問題