Tooltips for NSTableView cell’s in Tiger
June 10th, 2005 at 8:37 am (trackback)
At WWDC, I quickly mentioned how easy it is to add tooltip’s to an NSCell for an NSTableView/NSOutlineView.
Here is a quick snippet of code on how to do this only if the text doesn’t fill up the entire cell:
- (NSString *)tableView:(NSTableView *)tv toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)tc row:(int)row mouseLocation:(NSPoint)mouseLocation {
if ([cell isKindOfClass:[NSTextFieldCell class]]) {
if ([[cell attributedStringValue] size].width > rect->size.width) {
return [cell stringValue];
}
}
return nil;
}
You will obviously have to set the delegate for the tableview to be whatever class implements the above method, and this will only work on Tiger. But, it is REALLY easy to do.
This entry was posted
on Friday, June 10th, 2005 at 8:37 am and is filed under Apple, Cocoa, General.
You can follow any responses to this entry through the
RSS 2.0 feed.
You can leave a response, or
trackback
from your own site.








February 3rd, 2006 at 5:59 pm
I’ve just discovered this method, which would be extremely useful for me. However I can’t seem to get it to function properly (no tooltips are displaying). I’ve set the appropriate delegate – are there any other attributes that need to be set to enable this?
February 4th, 2006 at 10:44 am
Lou: it should just work…of course, it has to be Tiger or better. Make sure you have the delegate set in addition to the datasource; a common mistake is to forget to set the delegate.
February 6th, 2006 at 7:58 pm
Thanks, it was a delegate issue. The code works as advertised.