2009-04-19 9 views
6

Ho un NSAttributedString che sto utilizzando in un NSTextFieldCell. Crea diversi collegamenti URL cliccabili e inserisce una grande NSAttributedString all'interno di NSTextFieldCell. Ogni volta che visualizzo NSTextFieldCell normalmente ed è evidenziato, non posso fare clic sui collegamenti.Collegamento URL cliccabile in NSTextFieldCell all'interno di NSTableView?

Se imposto il TableView in modo da poter modificare ogni colonna o riga, quando faccio clic due volte, andare in modalità Modifica e visualizzare i contenuti NSTextFieldCell, i miei collegamenti appaiono e sono cliccabili. Quando clicco fuori dalla riga, non riesco più a vedere i link cliccabili.

Devo essere in modalità "modifica" per visualizzare i collegamenti o fare clic su di essi.

Mi sento come se ci fosse un ambiente che mi manca.

risposta

1

non credo che la nota tecnica risponde alla domanda, che è stato come mettere un link in una cella di NSTableView. Il modo migliore che ho trovato per fare questo è usare una cella pulsante per la cella della tabella. Ciò presuppone che solo i collegamenti si troveranno in una particolare colonna della tabella.

In Interface Builder, trascinare una cella NSButton sulla colonna della tabella in cui si desidera i collegamenti.

Nella vista tabella delegato, implementare tableView: dataCellForTableColumn: consecutive: come segue:

- (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); 
} 

Impostare l'obiettivo/azione per il tavolo per il delegato e verificare per i clic sulla colonna di link:

- (void) clickTable: (NSTableView *) sender 
{ 
    NSTableColumn *column = [[sender tableColumns] objectAtIndex:[sender clickedColumn]]; 
    NSInteger row = [sender clickedRow]; 
    NSString *link = nil; 

    if ([self isLinkColumn:column] && (link = [self linkForRow:row]) != nil) 
     [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:link]]; 
} 

Ora il collegamento sembra un collegamento come un collegamento, ma un clic su di esso è in realtà una pressione di un pulsante, che viene rilevato nel metodo di azione e invio tramite NSWorkspace.