Re: Best way to display dictionary in a table
Re: Best way to display dictionary in a table
- Subject: Re: Best way to display dictionary in a table
- From: Rick Hoge <email@hidden>
- Date: Thu, 18 Aug 2005 18:18:11 -0400
What is the 'right' way to display the contents of a dictionary
and allow users to edit the values (assuming they are strings)?
Build an array controller that accesses the dictionary, I would
assume.
Then you will be able to bind the table columns to the array
controller.
No, it's not this simple. You have to build in effect a dictionary
to array transformer. You need a way to arrange the keys and
values of the dictionary into an array that an array controller can
then use. One approach is to create a KeyValue class that has two
variables, (obviously) key and value, and to create instances of
these for each key-value pair in the dictionary. You then create
an array of all these instances to pass to the array controller.
The difficulties come in various forms: you must ensure you keep a
mapping between the KeyValue instances and the corresponding key-
value pairs in the dictionary; you must ensure that if a user edits
a key, or creates a new key, that it is not the same as an existing
key; you must be prepared to deal with sorting (see first point);
etc. It's not desperately complex, but it's not trivial either.
Ok thanks for this comment - it was pretty easy to get read-only
access to the dictionary using a value transformer. The transformer
replaces the dictionary with an array of dictionaries, which in turn
each have the two keys "key" and "value" (for the corresponding
objects). The problems now is to get the original dictionary updated
when the user edits objects - this does not seem to happen
automatically, even though as far as I can tell the table column
contains the same object that is in the original dictionary. Below
is an excerpt from the value transformer I wrote - I'll keep slogging
away at this but if anyone has a clean idea on how to get editing of
the columns to propagate like normal bindings I'd love to hear it!
Rick
+(Class)transformedValueClass { return [NSMutableArray class]; }
+(BOOL)allowsReverseTransformation { return YES; }
-(id)transformedValue:(id)value {
if (value == nil) return nil;
// Attempt to get a reasonable value from the
// value object.
NSArray *keys = nil;
if ([value respondsToSelector: @selector(allKeys)]) {
//zeroOffsetInputValue = [value intValue];
keys = [value allKeys];
} else {
[NSException raise: NSInternalInconsistencyException
format: @"Value (%@) does not respond to -allKeys.",
[value class]];
return nil;
}
NSMutableArray *newArray = [NSMutableArray array];
NSEnumerator *en = [keys objectEnumerator];
id nextKey = nil;
while (nextKey=[en nextObject]) {
NSArray *objects = [NSMutableArray arrayWithObjects:nextKey,
[value objectForKey:nextKey],nil];
NSArray *keys = [NSMutableArray
arrayWithObjects:@"key",@"value",nil];
[newArray addObject:[NSMutableDictionary
dictionaryWithObjects:objects forKeys:keys]];
}
return newArray;
}
_______________________________________________
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