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.
Nguồn
2011-06-14 20:36:57
Liên kết này không còn hoạt động nữa – Fatlad