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 20:27:04 -0400
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 you are right about needing a class!
You can transform the dictionary into an array of dictionaries if you
just want read-only display of the dictionary. However if you want
to be able to edit the dictionary values from the table you need to
use an array of objects (I called the class DictionaryEntry). In
addition to "key" and "value" attributes, the class has a "parent"
attribute which refers to the dictionary to which the entry belongs.
By binding the value attribute of the entry object to the value in
the parent dictionary, you can make sure that updates elsewhere are
reflected in your table display:
-(id)initWithParent:(NSMutableDictionary*)theParent key:(id)theKey
value:(id)theValue {
parent = [theParent retain];
key = [theKey retain];
value = [theValue retain];
[self bind:@"value" toObject:parent withKeyPath:theKey options:nil];
return self;
}
By using the following custom 'set' method, you cause edits to the
value in the table to be applied to the parent dictionary:
-(void)setValue:(id)newValue {
[newValue retain];
[value release];
value = newValue;
// Update the parent dictionary, triggering notification
[parent setObject:newValue forKey:key];
}
I think the rest of the functionality should be straightforward -
thanks for suggesting this route.
Rick
_______________________________________________
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