Re: Key-value coding dynamically-created arrays in NSTableView
Re: Key-value coding dynamically-created arrays in NSTableView
- Subject: Re: Key-value coding dynamically-created arrays in NSTableView
- From: Quincey Morris <email@hidden>
- Date: Thu, 14 Jul 2011 17:31:07 -0700
On Jul 14, 2011, at 17:08, John Bartleson wrote:
> Here the column identifier specified in the nib is used to select which ChildObject accessor
> to use as the source for the column's data.
>
> But suppose the columns of the tableView are not defined in the nib. Instead, they are defined
> dynamically by user action at run time. It's easy to dynamically add columns to the tableView and to
> give each one a unique identifier , but is it even possible to somehow assign key names to elements
> in a dynamic data array so valueForKey: can find those elements?
>
> This issue is made more difficult because the technique valueForKey uses to select an accessor is not
> apparent, at least to a n00b like me. For extra points, can anyone describe how valueForKey works?
If you write '[child valueForKey:@"xxx"]', it eventually invokes 'child.xxx'.
So, if you're adding columns that correspond to *known* ChildObject properties, you know you must use the property name as the column identifier, if you want your technique to work. This is the same whether the columns are created in the nib, or created later in code.
If you're saying you want to "add" arbitrary properties to ChildObject at run-time, that's a bit harder. One approach is to use a NSDictionary instead of a ChildObject, which responds to 'valueForKey:@"xxx"' by looking up 'objectForKey:@"xxx"'. But that only works for very simple cases.
Another approach is to have the ChildObject class override 'valueForUndefinedKey:'. In your override, you figure out if the undefined key is something you can come up with a value for. If not, you can just call super to get the normal exception.
As you can see, the answer you need depends on the question you're asking. Are you just trying to add columns at run-time, or are you trying to "add" data model properties at run time?
_______________________________________________
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