number format in table view

Is it possible to use a number formatter control in a table view? I’ve tried dragging the formatter to the table view (and also selected a column in the view and tried dragging in a formatter). It doesn’t work.

I can’t possibly be the only person that wants to use a table for fixed decimal numeric data. Any help?:confused:

NSNumberFormatter doesn’t know how to talk to a table view, so you can’t use it like that. You have to attach it to an object that adheres to NSCell. NSTextField, for example, uses a subclass of NSCell to implement its interface.

You can’t do this with the cells of a table in Interface Builder, however, since they don’t exist until you fill the table with data. To attach a formatter to a table view, you have to get the data cell object for the column you wish to format and use -setFormatter: to attach an instance of NSNumberFormatter to it.

I don’t know if you can do this via pure AppleScript, though I suspect not. You can probably do it in AppleScript via “call method”, but it’s probably easier to do as much of it as you can in Cocoa.

Edit, summary:

  1. Get the table column you want to format: -tableColumnWithIdentifier: of NSTableView
  2. Get the column’s data cell: -dataCell of NSTableColumn
  3. Attach formatter: -setFormatter: of NSCell

[[[tableView tableColumnWithIdentifier:columnIdentifier] dataCell] setFormatter:theFormatter];


Thanks for the reply. Actually I’m mistaken - I can attach a formatter to a table column, I just wasn’t doing it right. You have to drag the formatter to the column header of the table column you want to attach it to, and then the column header outline will “lite up” and accept it.

I was trying to drag it into the data area, and since (in Interface Builder) there isn’t any data yet, it was “bouncing” the control back to the palette.

Just speaking from experience. They don’t seem to work as intended all the time that way. I just do it programmatically, as I’m usually writing the code that generates the numbers when I realize I need a formatter.