I have a table view with 4 columns, connected to a conventional datasource. The 
first 3 columns are fully set up in IB to have text field cells with attached 
number formatters. The 4th column has a variable data type where the user 
indirectly chooses a data type and the code dynamically sets the column's 
dataCell to the appropriate type for editing it. For a numeric value, this is a 
text field cell with an attached NSNumberFormatter, just like the other columns.

The problem I'm having is that when the user edits the value in this column, 
the object value passed to the datasource's method:

- (void) tableView:(NSTableView*)aTableView setObjectValue:anObject 
forTableColumn:(NSTableColumn*) aTableColumn row:(NSInteger) rowIndex;

is a string, not an NSNumber. From the columns set up in IB, I get NSNumbers as 
expected. It's as if the formatter is getting bypassed, though I can see it is 
definitely having the correct effect going the other way - it formats the field 
properly.

Obviously I'm missing something in the set-up of the cell, but what?

The cell type is set by this method, invoked when the user indirectly chooses 
the data type (some irrelevant code removed):



- (void)        setOutputType:(int) aType
{
        NSTableColumn* tc = [mBinsTable 
tableColumnWithIdentifier:@"representedObjectValue"];
        [[tc headerCell] setTitle:[self columnTitleForDataType:aType]];
                
        // the cell type must be set to be appropriate to the class of data 
expected from -representedObjectValue
        // for styles this is an image, colours = special colour cell, text and 
number = text cell, with attached formatter for numbers.
                
        NSCell* aCell = [self dataCellForDataType:aType];
        [aCell setEditable:YES];
        [aCell setEnabled:YES];
        [tc setDataCell:aCell];
                
        [mBinsTable reloadData];
}

The cell is made here (other type cases removed):



- (NSCell*)     dataCellForDataType:(int) aType
{
        NSCell* aCell = nil;
        
        switch( aType )
        {
                default:
                        break;

                case kBinOutputTypeValue:
                {
                        aCell = [[NSTextFieldCell alloc] init];
                
                        NSNumberFormatter* formatter = [[NSNumberFormatter 
alloc] init];
                        [formatter 
setNumberStyle:NSNumberFormatterDecimalStyle];
                        [formatter setFormat:@"##0.00"];
                        [aCell setFormatter:formatter];
                        [formatter release];
                }
                break;
        }
        
        return [aCell autorelease];
}

The table view uses a delegate, and overrides:



- (NSCell*)     tableView:(NSTableView*) tableView 
dataCellForTableColumn:(NSTableColumn*) tableColumn row:(NSInteger) row
{
#pragma unused(tableView)
        
        if( tableColumn == nil )
                return nil;
        
        if( [[tableColumn identifier] 
isEqualToString:@"representedObjectValue"])
        {
                if( row == mTempCellRow && row != -1 && mTempCell )
                        return mTempCell;
        }
        
        return [tableColumn dataCellForRow:row];
}

The 'mTempCellRow' stuff is used to deal with one of the custom cell types, not 
this one - I've verified that it doesn't take the wrong branch and just calls 
-dataCellForRow:

Finally, this is where I'm seeing the problem. This code includes a workaround 
that I have at present, but it's a band-aid. I'd rather it worked properly than 
need this:


- (void)        tableView:(NSTableView*)aTableView setObjectValue:anObject 
forTableColumn:(NSTableColumn*) aTableColumn row:(NSInteger) rowIndex
{
#pragma unused(aTableView)

        NSString* ident = [aTableColumn identifier];
        DKOBin* bin = [[mTransformer bins] objectAtIndex:rowIndex];
        
        // for some reason the raw string is passed instead of a number created 
by the formatter. For the time
        // being deal with this by performing the conversion manually here.
        
        if([ident isEqualToString:@"representedObjectValue"])
        {
                if([anObject isKindOfClass:[NSString class]] && [self 
outputType] == kBinOutputTypeValue)
                {
                        anObject = [NSNumber numberWithFloat:[anObject 
floatValue]];
                }
        }
        
        [bin setValue:anObject forKey:ident];
}


--Graham




_______________________________________________

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