Replacing text in NSTextFieldCell inside NSTableView
- by earl.ct
Whenever a user would type a number, my app would automatically prepend a currency sign before that number. For example, when the user types "1" in a text field, the text inside it becomes "$1.00". All is good when I use an NSNumberFormatter, an NSTextField, and its delegate method control:didFailToFormatString:errorDescription:.
- (BOOL)control:(NSControl *)control didFailToFormatString:(NSString *)string errorDescription:(NSString *)error
{
if ([[control formatter] isKindOfClass:[NSNumberFormatter class]])
{
NSNumberFormatter *formatter = [control formatter];
if ([formatter numberStyle] == NSNumberFormatterCurrencyStyle && !
[string hasPrefix:[formatter currencySymbol]])
{
NSDecimalNumber *new = [NSDecimalNumber decimalNumberWithString:string];
if (new == [NSDecimalNumber notANumber]) {
new = [NSDecimalNumber zero];
}
[control setObjectValue:new];
}
}
return YES;}
Now I would like to have this functionality when a user types a number in a cell inside an NSTableView. I tried using control:didFailToFormatString:errorDescription: but the cell would erase the text instead.