Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think the tech note answers the question, which was how to put a link in an NSTableView cell. The best way I've found to do this is to use a button cell for the table cell. This assumes that only links will be in a particular column of the table.</p> <p>In Interface Builder, drag an NSButton cell onto the table column where you want the links.</p> <p>In your table view delegate, implement tableView:dataCellForTableColumn:row: as follows:</p> <pre><code>- (NSCell *) tableView: (NSTableView *) tableView dataCellForTableColumn: (NSTableColumn *) column row: (NSInteger) row { NSButtonCell *buttonCell = nil; NSAttributedString *title = nil; NSString *link = nil; NSDictionary *attributes = nil; // Cell for entire row -- we don't do headers if (column == nil) return(nil); // Columns other than link do the normal thing if (![self isLinkColumn:column]) // Implement this as appropriate for your table return([column dataCellForRow:row]); // If no link, no button, just a blank text field if ((link = [self linkForRow:row]) != nil) // Implement this as appropriate for your table return([[[NSTextFieldCell alloc] initTextCell:@""] autorelease]); // It's a link. Create the title attributes = [[NSDictionary alloc] initWithObjectsAndKeys: [NSFont systemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName, [NSNumber numberWithInt:NSUnderlineStyleSingle], NSUnderlineStyleAttributeName, [NSColor blueColor], NSForegroundColorAttributeName, [NSURL URLWithString:link], NSLinkAttributeName, nil]; title = [[NSAttributedString alloc] initWithString:link attributes:attributes]; [attributes release]; // Create a button cell buttonCell = [[[NSButtonCell alloc] init] autorelease]; [buttonCell setBezelStyle:NSRoundedBezelStyle]; [buttonCell setButtonType:NSMomentaryPushInButton]; [buttonCell setBordered:NO]; // Don't want a bordered button [buttonCell setAttributedTitle:title]; [title release]; return(buttonCell); } </code></pre> <p>Set the target/action for the table to your delegate and check for clicks on the link column:</p> <pre><code>- (void) clickTable: (NSTableView *) sender { NSTableColumn *column = [[sender tableColumns] objectAtIndex:[sender clickedColumn]]; NSInteger row = [sender clickedRow]; NSString *link = nil; if ([self isLinkColumn:column] &amp;&amp; (link = [self linkForRow:row]) != nil) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:link]]; } </code></pre> <p>Now the link <em>looks</em> like a link, but a click on it is actually a button press, which you detect in the action method and dispatch using NSWorkspace.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload