基于NSOutlineView视图 - 仅在选中时显示按钮

时间:2014-03-30 17:39:16

标签: objective-c cocoa nsoutlineview

我有一个基于NSOutlineView视图的一列Header + image / text / button

我想仅在选择项目时显示按钮。

我使用以下代码:

- (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语句中的断点=>如果声明永不匹配

1 个答案:

答案 0 :(得分: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:当用户更改选择时。

谢谢!