On Jul 20, 2009, at 22:16, Daniel Child wrote:

I seem to have fallen into the pitfall you described below. I can get the table view to display values in a combo box from a data source, which takes those values from an array in the model. I also have autocomplete working.

But I can't seem to get the values I choose to "stick" (remain displayed) in the table. Since the tableview is reusing the combo box cell, I don't see how you specify a different value for each row (I only have one column). (The choice values do appear when you click the arrow, but selecting them does not store a value.) Also, I'm not clear on when you "capture" a selection to get the value transferred to the model.

Are you supposed to use some notification to know that the field has been edited, and then capture that value and transfer it to the model. Sorry if this seems basic, but I've looked over the documentation and some sample code and just don't get it.

Here's what I'd try:

-- I'd give each data model object a string property "stateName", and bind the table column to this property.

-- I'd write a "validateStateName:" method to check that the entered name was one of the names in my list of names. That'd probably a case- insensitive test, so I'd also use this method to replace the user's entry with a copy of the actual string from the list. (Make sure you turn on "validates immediately" for the binding. That means that the validation is done on Enter or Tab, not on every character typed as the name of the option might suggest.) Refer to the KVC documentation for information about how to write a validate<Key>: method.

If the user is allowed to enter names not in the list, then this method would validate non-list names for correctness (and do things like apply proper capitalization and replace the user entry with a prettified string) as desired.

-- I'd write a "setStateName:" setter to store the entered name in the instance variable backing the "stateName" property. Since this method could potentially be called for other reasons, I'd also re-apply the prettification here, so that the resulting value 'isEqualToString:' the matching string from your master list, if it's in your master list.

That's the general idea. The specifics would depend on whether:

-- the user was *only* allowed to enter things from your master list

-- the user was allowed to enter things not in the master list, which then get added to the master list

-- the user was allowed to enter arbitrary names, which are *not* added to the master list if unknown

If you need the model objects to store a property that's the numeric index into the master list, that can be done in the setter too. One of the properties would be made dependent on the other, but again the details depend on what behavior you specifically need to allow.

Note that this implementation (which is part of the data model) doesn't know that a NSComboxBox is being used to get the name -- a regular text field could be used just as easily (for you, though not for the user). That's as it should be.

HTH


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to