Re: Binding a dictionary to a table view
Re: Binding a dictionary to a table view
- Subject: Re: Binding a dictionary to a table view
- From: Stefan Fisk <email@hidden>
- Date: Tue, 1 Nov 2005 08:51:47 +0100
31 okt 2005 kl. 22.11 skrev Scott Ellsworth:
On Oct 31, 2005, at 1:06 PM, John C. Randolph wrote:
On Oct 31, 2005, at 10:20 AM, Scott Ellsworth wrote:
On Oct 31, 2005, at 4:43 AM, John C. Randolph wrote:
On Oct 30, 2005, at 10:28 AM, Stefan Fisk wrote:
I've considered making a value transformer, but It seems a bit
dodgy.
That's exactly how to do it. You'll need to transform the
dictionary into an array of objects that represent key-value
pairs. Bill Bumgarner showed me how to go about this some time
ago. He implemented a value transformer and a KeyValuePair
class which had keys for "key" and "value". -transformedValue:
returned an array of these KeyValuePair objects.
What did you do about ordering?
Nothing.
I had thought that the order of keys in a dictionary was
arbitrary, and thus could change as objects were added/deleted.
That's correct.
Don't mean to be obtuse, but would that not be a real problem for a
table view? Put another way, did you not allow object addition/
deletion, or did you somehow refresh the entire view if an item was
added/deleted?
I am asking questions because this seems to fit a situation I am
in. I have a set of object that absolutely must be in a dictionary
so lookups for display are fast, but that are also displayed as an
array in a tableview. A value transformer sounds like a good way
to go from two structures to one, but I do not want it to suddenly
bite me in the future if someone adds an item.
Actually, I got it to work before i even got his reply, it's that
simple. What happens when the dictionary is updated is that the table
view re-reads its whole content, so all keys get represented. As for
sorting, the easiest thing to do is to let the table view sort
itself. As soon as anything is changed from the table view's side it
re-write's the whole dictionary anyway, so nothing is lost, any
everything works just fine. Here's the important bits of my transformer:
- (id)transformedValue:(id)value {
NSMutableArray *array = [NSMutableArray array];
foreach (NSString *key, [value allKeys]) {
[array addObject:[NSDictionary dictionaryWithObjectsAndKeys:
key, @"key",
[value valueForKey:key], @"value", nil]];
}
return array;
}
- (id)reverseTransformedValue:(id)value {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
foreach (NSDictionary *keyValuePair, value) {
[dictionary setValue:[keyValuePair valueForKey:@"value"]
forKey:[keyValuePair valueForKey:@"key"]];
}
return dictionary;
}
_______________________________________________
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