मुझे नहीं लगता कि तकनीक टिप्पणी सवाल है, जो कि कैसे एक NSTableView सेल में एक लिंक डाल करने के लिए था उत्तर देता है। ऐसा करने का सबसे अच्छा तरीका यह है कि तालिका कक्ष के लिए एक बटन सेल का उपयोग करना है। यह मानता है कि केवल लिंक तालिका के एक विशेष स्तंभ में होंगे।
इंटरफ़ेस बिल्डर में, एनएसबटन सेल को तालिका कॉलम पर खींचें जहां आप लिंक चाहते हैं।
अपने तालिका दृश्य प्रतिनिधि में, 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 का उपयोग करके प्रेषण करते हैं।
स्रोत
2011-06-14 20:36:57
लिंक अब और काम नहीं कर रहा है – Fatlad