Re: insert a plist in a NSTableView using a NSDictionary
Re: insert a plist in a NSTableView using a NSDictionary
- Subject: Re: insert a plist in a NSTableView using a NSDictionary
- From: mmalc crawford <email@hidden>
- Date: Sun, 23 Sep 2007 14:05:51 -0700
On Sep 23, 2007, at 12:25 PM, NSTask wrote:
I am trying to insert a plist on to a NSTableView. So
I have two columns in tableView, one for Key and the
other for Value.
[...]
You don't need this, just bind the table column with
nsarraycontroller and your dictionary to nsarray controller as an
object.
When an obvious beginner is asking a question about how to use a table
view data source, it's asinine to simply give a general reference to
bindings in response -- see <http://lists.apple.com/archives/Cocoa-dev/2007/Sep/msg00956.html
>. Using a table view data source is a perfectly good way to start
with Cocoa, in particular to learn about a number of design patterns
that will be useful in all aspects of Cocoa development.
It's even more misleading if bindings are inappropriate for the
problem at hand. There is no straightforward way to manage the keys
and values of a *dictionary* using an array controller.
plistDictionary_ = [NSDictionary
dictionaryWithContentsOfFile:kPlistPath];
How will I load this NSDictionary onto the
NSTableView?
What should be the return value in
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn
*)tableColumn
row:(int)row {
The difficulty lies in the fact that you're dealing with an unordered
dictionary as opposed to an ordered array.
You need some means to impose order on the dictionary. Typically
you'd do this by sorting the keys (first, keys are what you use as the
entry point into the dictionary; second, within the dictionary they
are unique -- this may not be the case for the values).
An inefficient but explicit implementation might look like this...
mmalc
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [[plistDictionary allKeys] count];
}
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
NSArray *keysArray = [plistDictionary allKeys];
NSArray *sortedArray =
[keysArray
sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
id keyAtRow = [sortedArray objectAtIndex:row];
// this assumes you have given the key column the identifier,
"keyColumn"
if ([[tableColumn identifier] isEqualToString:@"keyColumn"])
{
return keyAtRow;
}
// assume no other columns
return [plistDictionary objectForKey:keyAtRow];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden