Re: NSTableView, NSNumberFormatter, and other fun.
Re: NSTableView, NSNumberFormatter, and other fun.
- Subject: Re: NSTableView, NSNumberFormatter, and other fun.
- From: Chris Giordano (at glue) <email@hidden>
- Date: Fri, 25 Jan 2002 12:13:11 -0500
Greg,
(Something I know for once.)
I didn't find a method for doing this in IB either. However, you can do
this programmatically by implementing the NSTableView delegate method
tableView:willDisplayCell:forTableColumn:row:.
I have a table view with a date and a numeric column. In IB I set the
identifier to some identifiable values (in this case "thedate" for the
date column, and "amt" for the numeric column). I have the formatters
as outlets of my delegate class in the nib file.
My delegate method looks like the following. It is called for each cell
in the table. Basically, the idea is that if the cell should have a
formatter, it gets set here. If it shouldn't have a formatter, then it
gets set to nil.
(This method is documented in the NSTableView documentation, and I think
I was pointed to this solution from some example code, but I can
remember exactly where off the top of my head, nor can I find it on a
cursory search.)
- (void)tableView:(NSTableView *)theTableView
willDisplayCell:(id)theCell forTableColumn:(NSTableColumn *)theColumn
row:(int)rowIndex
{
if ([[theColumn identifier] isEqualToString:@"thedate"])
[theCell setFormatter:_theDateFormatter];
else if ([[theColumn identifier] isEqualToString:@"amt"])
[theCell setFormatter:_theNumberFormatter];
else
[theCell setFormatter:nil];
}
Works for me. Hope it works for you.
chris
On Thursday, January 24, 2002, at 10:56 PM, Greg Anderson wrote:
Being relatively new to Cocoa (Obj-C, to be specific), hopefully
these aren't obvious questions.
I've got an NSTableView that has a column of temperatures as the
2nd
column. I'd like to be able to format the data in this column to a
certain number of decimal places, which sounds like the job of an
NSNumberFormatter. I can't seem to find a way to specify this via
Interface Builder. Am I missing something, or is this done
programmatically-only (or to reveal even more ignorance on my part, is
it
possible)?
In a possibly related thread, I have a couple of functions in my
dataSource for the table view that iterate over the data and return the
max and min temperatures. It works fine until I change a value in the
table (via typing into a cell in the Temperature column). Then my
comparison stops working at this line:
if ([maxTemp compare:testNumber] == NSOrderedAscending) {
Where maxTemp is an NSNumber* and testNumber is an NSNumber* that I
get from
testNumber = [theRecord objectForKey:@"Temp"];
and theRecord is an NSDictionary pulled from an NSMutableArray, the
kind of stuff one expects when working with the data for a table view.
The error I'm getting reported is
-[NSCFString objCType]: selector not recognized
I'm assuming that this is because when I get testNumber, it's an
NSString, and NSString doesn't understand -compare ... maybe ... ?
I'm getting around it by comparing the [maxTemp floatValue] to
[testNumber floatValue], but that seems awfully kludgey. So I'm not sure
if I'm missing something obvious in the world of Obj-C, or if I'm
missing
the way to make sure that values entered into a cell in a table view are
of the type I want them to be.
Any help is greatly appreciated. TIA.
Greg