2013-06-18 24 views
1

我最近安裝了山獅,並注意到我現在有數據綁定的Mac OS X應用程序正在顯示一個int32 NSTableViewCell作爲x,xxx。NSTableViewCell顯示,爲int

IE:.storedata文件顯示<attribute name="itemid" type="int32">2533</attribute>,而項目在單元格中顯示爲2,533。我無法弄清楚爲什麼它在山獅會做到這一點,但沒有在獅子山做。

如何獲取細胞,以顯示而不是作爲25332,533

enter image description here

回答

1

我結束了添加值轉換

@implementation ItemIdValueTransformer 

+(Class)transformedValueClass 
{ 
    return [NSString class]; 
} 

+ (BOOL)allowsReverseTransformation 
{ 
    return NO; 
} 

- (id)transformedValue:(id)value 
{ 
    // Remove the ,'s from Mountain Lion and up 
    NSString *string = [NSString stringWithFormat:@"%li",(long)[value integerValue]]; 
    return string; 
} 

@end 
1

這在一定程度上記錄在10.8版本說明:

的NSString定域格式化

在10.8中,在與10.8 SDK鏈接的應用程序中,-localizedStringWithFormat:和-initWithForm at:locale :(和朋友)當提供非零語言環境時,現在將執行數字的本地化格式化。以前這些調用已經做了小數點處理;所以在某些語言環境中,逗號將用於小數點分隔符。這種新的行爲建立在此基礎上,使用本地化數字以及數千個分隔符和正確的符號位置。

但我仍然認爲它是一個錯誤(並提交),它有很多問題(與傳統的UI搞砸,偶爾在手工編輯時出現錯誤的行爲等)。

對於新的用戶界面,您可能最適合將數字格式化程序添加到您的筆尖,用於顯示數字的所有文本字段。

如果(像我的情況),你有更多的文本字段許多筆尖文件,這個醜陋的黑客可能會有所幫助:

#import "HHUTextFieldCell.h" 

@implementation HHUTextFieldCell //:: NSTextFieldCell 

//***************************************************************************** 
// Class methods 
//***************************************************************************** 
+ (void)load { 

    // 
    // 10.8 started using thousands separators for text fields. For our legacy 
    // apps we don't want those. Rather than changing dozens of xib files with 
    // hundreds of text fields, we use a replacement class to modify the text 
    // fields to not have a thousands separator. 
    // 
    [NSKeyedUnarchiver setClass:[HHUTextFieldCell class] forClassName:@"NSTextFieldCell"]; 
} 

//***************************************************************************** 
// Overwritten methods 
//***************************************************************************** 
- (void)setObjectValue:(id <NSCopying>)object { 

    // 
    // If `object` is an NSNumber object and no formatter is set, we instead 
    // set the description of that number via -setStringValue:. Otherwise 
    // use the original implementation. 
    // 
    if(!self.formatter && [(NSObject *)object isKindOfClass:[NSNumber class]]) 
    { 
     [super setStringValue:[(NSObject *)object description]]; 
    } 
    else 
    { 
     [super setObjectValue:object]; 
    } 
} 

@end