Re: rows of pop up button cells with unique menus
Re: rows of pop up button cells with unique menus
- Subject: Re: rows of pop up button cells with unique menus
- From: "R. Matthew Emerson" <email@hidden>
- Date: Wed, 20 Sep 2006 22:13:30 -0400
On Sep 20, 2006, at 3:06 PM, Fritz Anderson wrote:
On Sep 20, 2006, at 1:57 PM, R. Matthew Emerson wrote:
I would think that I need each pop up button cell to have its very
own menu, but I can't quite see how to arrange this.
What happens if you provide a NSTableView delegate method
tableView:willDisplayCell:forTableColumn:row: that populates the
cell with that row's menu items and selection?
I thought I'd try to answer this question. The key hint for me was
the "and selection" part.
Here's what I did that seems to work. I'm not sure it's a great way
to do it, but since I don't expect to have thousands of rows, it
probably doesn't matter.
Each row of the table is represented by an NSDictionary instance.
The keys in the dictionary are name (an NSString), email (an NSArray
of NSStrings), and selectedEmail (an NSNumber).
The data for the rows are stored in an NSArray called tableRows,
which is initially populated via an LDAP query in another method.
(Sorry if this is obvious to everyone.)
- (void)tableView:(NSTableView *)tv
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
if ([[tableColumn identifier] isEqualToString:@"email"]) {
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"foo"];
NSDictionary *dict = [tableRows objectAtIndex:row];
NSArray *addrs = [dict objectForKey:@"email"];
int i;
NSNumber *n;
for (i = 0; i < [addrs count]; i++) {
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:
[addrs objectAtIndex:i]
action:NULL
keyEquivalent:@""];
[menu addItem:item];
[item release];
}
[cell setMenu:menu];
[menu release];
if ([addrs count] > 0) {
n = [dict objectForKey:@"selectedEmail"];
if (n != nil) [cell selectItemAtIndex:[n intValue]];
}
}
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSDictionary *dict = [tableRows objectAtIndex:rowIndex];
[dict setValue:anObject forKey:@"selectedEmail"];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
/* identifier is one of: name, email, certificate */
NSString *key = [aTableColumn identifier];
NSDictionary *dict = [tableRows objectAtIndex:rowIndex];
if ([key isEqualToString:@"certificate"]) {
id c = [dict objectForKey:key];
if (c) {
return @"present";
} else {
return @"none";
}
} else if ([key isEqualToString:@"email"]) {
SecCertificateRef c = (SecCertificateRef)[dict
objectForKey:@"certificate"];
NSArray *addrs;
if (c) {
addrs = [self subjectAlternativeNamesFromCertificate:c];
if ([addrs count] > 0) {
return [dict valueForKey:@"selectedEmail"];
} else {
return nil;
}
}
} else {
return [dict objectForKey:key];
}
return nil;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden