A custom cell maybe?


I have a map made of a square matrix of cells, each of them can have a specific color.

I have defined a “palette” to set up the color of a “pencil” to draw on this map. This palette should be modifiable by the user in two ways:

  • redefine a color using the color picker;
  • rename the legend of the color.

Currently I make this in a very modeless way, having for each possible color, from left to right:

  • a radio button, which is ON if this color is selected;
  • a color well, to change the color with the color picker;
  • a text field, to rename the legend.

   +---+    +----------------------+

(¢) | | | Legend name |
±–+ ±---------------------+

All is perfectly working, but tedious: First of all, I create a NSDictionary, which contains my color wells as properties, bound in IB.

When I click on a cell of my map, I don’t actually change its color. If the user redefines this color, the map drawn so far will not be updated. So I set the tag of this cell to the same value as the tag of the selected radio control: this becomes a color index (or better, a color reference)

When a cell redraws itself, it looks at its tag, and first converts it to a string. Then it uses this string as a key in the NSDictionary, gets the associated color well and sets its color background to this color well’s color.

After the user has changed some colors, he clicks on a “Refresh” button and the map is redrawn with the new colors. So far, so good.

I was wondering if it would be worth it to implement this differently, but I fear it would be in Objective-C: replace the three controls (radio, color well and text field) by a custom cell, placed into a table view.

This custom cell would present a colored square (bigger than the present color well, which is tiny when bordered) and the legend as its title.

The radio button would become useless, because the cell would be selected in the table view.

To redefine color and legend, the user double-clicks on the cell, and sets the new values into a dialog. The map is refreshed when this dialog is dismissed, so the “Refresh” button becomes useless too.

This second implementation is more modal than the first one, but maybe simpler to implement. The custom cell would retain, as instance variables, all that’s needed:

  • the color index (tag)
  • the actual color, more visible (background color)
  • the legend (title)

more, it could be bound in IB via the array controller of the table view.

With my precedent OOP language, I would have done this in 15 minutes but. this is the time of Objective-C. And I can’t see how to do this without subclassing NSCell.

Any other ideas?


The posts I’ve read on the Web about subclassing NSCell told it was a nightmare. OK. I drop this idea for the moment.