1

I have an example application I'm working on to help me learn about Core Data. In this application I created a model consisting of the entities "Friend" and "City". The application list my friends and which city they are from in an NSTableView. In this table view I would like to have the City column be an NSComboBoxCell with a list of the cities. I got this far... now for the problem:

When I select the city from the combo box, the application takes the value of the selected city name and applies it to the name of the city the friend is currently from. Instead, I would like the application to actually change the city the user is from and not the name of the city... That's a bit confusing of a question, so here's an example: starting with a list of friends like

Andy      Asheville
Francois  Montreal
Jeff      Asheville

If I use the NSComboBoxCell to change the city for Andy from Asheville to Montreal, the application actually changes the name of the City Asheville to Montreal, so the result looks like:

Andy      Montreal
Francois  Montreal
Jeff      Montreal

There are still two distinct cities in the application, but they now both have the name Montreal.

This all makes sense to me given the way I set up my bindings. I bound the value of the city table column by setting Model Key Path to "city.name" and the Controller Key to arrangedObjects, which contains the list of friends. So of course, when the value of a cell changes it modifies city.name. So then my question becomes what's the proper way to do this so that the city changes instead of the city name?

Jason
  • 11,709
  • 9
  • 66
  • 82

1 Answers1

3

The purpose of NSComboBox is to "allow you to either enter text directly (as you would with an NSTextField) or (...) select from a displayed list of items". (taken from Apple Cocoa Ref).

So The combo box is a glorified NSTextField. The behavior you expect is more in line with the NSPopupButton.

As you noted, the NSComboBox bindings change the name value of the current City, but do not alter the object relationship, hence they change the name of the current city represented by the relationship (which then gets "propagated" to other Friends pointing to the same City entity).

If you look at the available bindings for NSPopupButton you'll see the difference. You probably want to use the NSPopupButton(Cell) to assign Cities to Friends, with some sort of "on the side", NSTableView-based editor to manage your city names — which are really two distinct tasks.

burton
  • 86
  • 6
  • Great, thanks! I did just that and things work great. It would be nice if the user can type the name of the city instead of selecting it with the mouse. And the system could use something like autocompletion. – Jason Jun 30 '11 at 13:41
  • Well, at this point we're getting away from the original question and into design issues but you could go back to the NSComboBox and investigate the [completion functions](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSComboBoxCell_Class/Reference/Reference.html%23//apple_ref/doc/uid/20000098-BAJEAFCB), but that will require code. Also an interesting discussion [here](http://lists.apple.com/archives/cocoa-dev/2006/Jun/msg00459.html). – burton Jul 01 '11 at 04:33