2009-04-19 25 views
6

Tôi có một NSAttributedString mà tôi đang sử dụng trong một NSTextFieldCell. Nó tạo ra một số liên kết url có thể nhấp và đặt một NSAttributedString lớn bên trong NSTextFieldCell. Bất cứ khi nào tôi xem NSTextFieldCell bình thường và nó được đánh dấu, tôi không thể nhấp vào các liên kết.Liên kết url có thể nhấp trong NSTextFieldCell bên trong NSTableView?

Nếu tôi đặt TableView để tôi có thể chỉnh sửa từng cột hoặc hàng, khi tôi nhấp hai lần, vào chế độ Chỉnh sửa và xem nội dung NSTextFieldCell, liên kết của tôi hiển thị và có thể nhấp. Khi tôi nhấp chuột ra khỏi hàng, tôi không thể thấy các liên kết có thể nhấp nữa.

Tôi phải ở chế độ "chỉnh sửa" để xem các liên kết hoặc nhấp vào chúng.

Tôi cảm thấy có một số cài đặt mà tôi vừa bỏ lỡ.

Trả lời

1

Tôi không nghĩ rằng các lưu ý kỹ thuật trả lời câu hỏi, đó là làm thế nào để đặt một liên kết trong một tế bào NSTableView. Cách tốt nhất tôi đã tìm thấy để làm điều này là sử dụng một ô nút cho ô bảng. Điều này giả định rằng chỉ các liên kết sẽ nằm trong một cột cụ thể của bảng.

Trong bộ dựng giao diện, hãy kéo ô NSButton vào cột bảng nơi bạn muốn liên kết.

Trong bảng điểm đại biểu của mình, thực hiện tableView: dataCellForTableColumn: row: như sau:

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

Đặt mục tiêu/hành động cho bảng để đại biểu và kiểm tra cho các nhấp chuột vào liên kết cột:

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

Bây giờ liên kết trông giống như một liên kết, nhưng nhấp chuột vào nó thực sự là nút bấm, mà bạn phát hiện trong phương thức hành động và gửi đi bằng cách sử dụng NSWorkspace.

Các vấn đề liên quan