基于单元格的NSTableView的工具提示

时间:2014-05-28 08:17:38

标签: objective-c macos cocoa

我有一个视图(在其他元素中)包含基于单元格的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;

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

我的真正目标是用dinamycically定义我的细胞的工具提示。

1 个答案:

答案 0 :(得分:0)

我只需要为我的应用程序的开发解决这个问题。 您在上面提到的这些委托方法仅适用于文本单元格的扩展工具提示,在截断时显示全文。 一种可能的解决方案是添加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:移动或调整表格列时。

相关问题