2009-04-19 9 views
6

मेरे पास एक NSAttributedString है जिसे मैं NSTextFieldCell में उपयोग कर रहा हूं। यह कई क्लिक करने योग्य यूआरएल लिंक बनाता है और NSTextFieldCell के अंदर एक बड़ा NSAttributedString डालता है। जब भी मैं सामान्य रूप से NSTextFieldCell देख रहा हूं और इसे हाइलाइट किया गया है, तो मैं लिंक पर क्लिक नहीं कर सकता।NSTextFieldCell में NSTableView के अंदर क्लिक करने योग्य यूआरएल लिंक?

यदि मैं टेबल व्यू सेट करता हूं तो मैं प्रत्येक कॉलम या पंक्ति को संपादित कर सकता हूं, जब मैं दो बार क्लिक करता हूं, संपादन मोड में जाता हूं और NSTextFieldCell सामग्री देखता हूं, तो मेरे लिंक दिखाए जाते हैं और क्लिक करने योग्य होते हैं। जब मैं पंक्ति से दूर क्लिक करता हूं, तो मैं क्लिक करने योग्य लिंक नहीं देख सकता।

मुझे लिंक देखने या उन पर क्लिक करने के लिए "संपादन" मोड में होना होगा।

मुझे लगता है कि कुछ सेटिंग है जो मैं अभी याद कर रहा हूं।

उत्तर

1

मुझे नहीं लगता कि तकनीक टिप्पणी सवाल है, जो कि कैसे एक 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 का उपयोग करके प्रेषण करते हैं।

संबंधित मुद्दे