【问题标题】:Tooltip for Cell-based NSTableView基于单元格的 NSTableView 的工具提示
【发布时间】:2014-07-17 08:35:09
【问题描述】:

我有一个视图(在其他元素中)包含一个基于单元格的 NSTableView。我将 ViewController 设为表的代表。视图控制器以编程方式创建并添加到 NSStatusItem。

我实现了:

- (BOOL)tableView:(NSTableView *)tableView shouldShowCellExpansionForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    return YES;
}

- (NSString *)tableView:(NSTableView *)aTableView toolTipForCell:(NSCell *)aCell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation {
    return @"A tooltip";
}

- (BOOL)tableView:(NSTableView *)tableView shouldTrackCell:(NSCell *)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    return YES;
}

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
    NSLog(@"Display");
}

但是当我运行我的代码时,唯一被调用的方法之一是:

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex;

知道可能出了什么问题吗?

我的真正目标是动态地为我的单元格定义工具提示。

【问题讨论】:

  • 嵌入视图的 NSMenuItem 是否可能正在吞噬工具提示事件?

标签: objective-c macos cocoa


【解决方案1】:

我只是需要为我的应用程序开发解决这个问题。 您上面提到的这些委托方法仅适用于文本单元格的扩展工具提示,在截断时显示全文。 一个可能的解决方案是添加一个 NSViewFrameDidChangeNotification 的观察者,其中工具提示矩形使用 addToolTipRect:owner:userData: 重新排列。代码是这样的:

NSTableView *tableView; // the target tableview

- (void)tableViewSizeDidChange:(NSNotification *)theNotification {
    [tableView removeAllToolTips];
    for (NSInteger row = 0; row < tableView.numberOfRows; row ++) {
        NSRect rowRect = [tableView rectOfRow:row];
        for (NSInteger col = 0; col < tableView.numberOfColumns; col ++) {
            NSRect colRect = [tableView rectOfColumn:col];
            NSRect cellRect = NSIntersectionRect(rowRect, colRect);
            [tableView addToolTipRect:cellRect owner:
                /* Tooltip string for cell at row and col */ userData:NULL];
    }}
}

- (void)windowDidLoad {
...
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(tableViewSizeDidChange:)
        name:NSViewFrameDidChangeNotification object:tableView];
...

您可能还需要调用 tableViewSizeDidChange: 当表格列被移动或调整大小时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2011-06-15
    相关资源
    最近更新 更多