2009-04-19 4 views
6

NSTextFieldCell에서 사용하고있는 NSAttributedString이 있습니다. 그것은 여러 개의 클릭 할 수있는 URL 링크를 만들고 NSTextFieldCell 안에 큰 NSAttributedString을 넣습니다. NSTextFieldCell을 정상적으로보고 강조 표시 될 때마다 링크를 클릭 할 수 없습니다.NSTableView 내부의 NSTextFieldCell에서 클릭 할 수있는 URL 링크?

각 열이나 행을 편집 할 수 있도록 TableView를 설정하면 두 번 클릭하면 편집 모드로 들어가 NSTextFieldCell 내용을 볼 수 있으며 링크가 나타나고 클릭 할 수 있습니다. 행을 클릭하면 더 이상 클릭 할 수있는 링크가 표시되지 않습니다.

링크를 보거나 클릭하려면 "편집"모드 여야합니다.

내가 놓친 것 같은 설정이있는 것처럼 느껴집니다.

답변

1

나는 테크 노트는 NSTableView는 셀에 링크를 넣어하는 방법이었다 질문, 답변을 생각하지 않습니다. 이 작업을 수행하는 가장 좋은 방법은 표 셀에 단추 셀을 사용하는 것입니다. 이 경우에만 링크가 테이블의 특정 열에 있다고 가정합니다.

Interface Builder에서 NSButton 셀을 링크를 원하는 테이블 열로 끌어 놓습니다.

테이블 뷰 위임에

,있는 tableView를 구현 : dataCellForTableColumn : 행을 다음과 같이 :

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

대리인에 테이블의 대상/액션을 설정하고 링크 열에 클릭 확인 :

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

링크 은 링크처럼으로 보입니다. 실제로이 버튼을 클릭하면 동작 방법에서 감지하고 NSWorkspace를 사용하여 디스패치합니다.