2015-10-07 33 views
-3

在簡單的TableViewController項目中出現錯誤,因爲它看起來像我試圖強制對象作爲cellForRowAtIndexPath方法的一部分,被稱爲CheckValue.text的標籤的文本。[NSDecimalNumber Length]:發送到實例的無法識別的選擇器

事實上,我正在努力做到這一點,因爲我需要將NSDecimalNumber的值顯示在TableCell上。任何人都可以幫忙,因爲我覺得我在這裏接近子彈嗎?

下面是從TableViewController.m代碼

#import "TableViewController.h" 
#import "TableCell.h" 

@interface TableViewController() 
{ 
    NSMutableData *webData; 
    NSURLConnection *connection; 
    NSMutableArray *array; 
} 

@end 

@implementation TableViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
    // Get an array from web and Populate Check Arrays with New Data as above ... 

    [self GetDatabaseData]; 
} 

-(void)GetDatabaseData{ 
    NSURL *blogURL = [NSURL URLWithString:JSON_URL]; 
    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL]; 
    NSError *error = nil; 

    //NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData: jsonData options:0 error:&error]; 
    NSArray *TransactionArray = [NSJSONSerialization JSONObjectWithData: jsonData options:0 error:&error]; 

    NSDecimalNumber *check_total = [NSDecimalNumber zero]; 

    NSString *current_check; 

    // Loop through Json objects, create question objects and add them to our questions array 
    for (int i = 0; i < TransactionArray.count; i++) 
    { 
     NSDictionary *jsonElement = TransactionArray[i]; 

     // Accumulate if this is part of the same check. 
     NSDecimalNumber *line_total = [NSDecimalNumber decimalNumberWithString:jsonElement[@"tran_value"]]; 

     // Decide if this is Element Belong to the Last Check number. 
     if (jsonElement[@"tran_check"] == current_check){ 
      check_total = line_total; // Reset Check Total to be the Value of this Line on NEW Checks 
     } 
     else{ 
      check_total = [check_total decimalNumberByAdding:line_total];  //Add this Line Value to the Running Check Total 
     } 

     current_check = jsonElement[@"tran_check"]; // Make this Check number the Current Check for Totalizing. 

     _Checknumbers = @[jsonElement[@"tran_check"]]; 
     _CheckTime = @[jsonElement[@"tran_hour"]]; 
     _CheckValue = @[check_total]; 
    } 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return _Checknumbers.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    static NSString *CellIdentifier = @"TableCell"; 

    TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    int row = [indexPath row]; 

    cell.CheckLabel.text = _Checknumbers[row]; 
    cell.CheckValue.text = _CheckValue[row]; 
    cell.CheckTime.text = _CheckTime[row]; 

    return cell; 
} 

@end 
+0

的[我如何調試「無法識別的選擇發送到實例」錯誤]可能的複製(http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-選擇器發送到實例錯誤) –

+0

嗨熱舔,我確實設法找到代碼行的問題,並知道必須做什麼,只是不知道這種轉換的語法。 – Rob4236

回答

-1

可以與NSDecimalNumber值

cell.CheckValue.text = [NSString stringWithFormat:@"%@",_CheckValue[row]] 
+0

感謝這個Boris,稍後我會回家檢查代碼。如果你可以幫助下一個問題: 正如你所看到的,我使用[i]循環了一個名爲TransactionArray的NSArray,但是在循環內你可以看到我正在填充3個元素的其他NSArrays的JSON元素。 喜歡這個: _Checknumbers = @ [jsonElement [@「tran_check」]]; _CheckTime = @ [jsonElement [@「tran_hour」]]; _CheckValue = @ [check_total]; 這當然意味着每個NSArrays只包含TransactionArray中元素的最後一個值.... – Rob4236

+0

如何將新值添加到循環中的每個數組而不是將數組的值替換爲這1個對象? – Rob4236

0

NSDecimalNumber繼承NSNumber所以NSNumberFormatter格式可以用來得到你需要的字符串表示。

下面是一個例子,變化,以滿足您的需求:

- (NSString *)formatNumber:(NSDecimalNumber *)number { 
    NSNumberFormatter *numberFormatter = [NSNumberFormatter new]; 
    [numberFormatter setMinimumFractionDigits:2]; 
    numberFormatter.usesGroupingSeparator = YES; 

    return [numberFormatter stringFromNumber:number]; 
} 

例子:

 
number  returns 
12456.3 12,456.30 
12   12.00 
12.3456  12.35 

我曾經在一個樣本計算器應用程序。

如果formatNumber方法添加到TableViewController類:

cell.CheckValue.text = [self formatNumber:_CheckValue[row]]; 

爲了方便調試(總是一件好事)

NSDecimalNumber *checkValueDecimal = _CheckValue[row]; 
NSString *checkValueString = [self formatNumber:_ checkValueDecimal]; 
cell.label.text = numberString; 

這樣一個可以檢查每個步驟中,編譯器將優化這在發佈版本上。

+0

我會如何使用這種方法? cell.label.text = [formatNumber:NSDecimalNumber * somenumber]; ? – Rob4236

+0

已更新的答案。 – zaph

0

的UILabel顯示類的NSString的對象格式的字符串,則作爲參數NSDecimalNumber傳遞,當設置的文本被檢查傳入值包括長度(Lenght方法),因爲該對象不支持此方法 - 拋出錯誤。解決方案 - 將NSDecimalNumber轉換爲NSString。 例如:

cell.CheckValue.text = [_CheckValue[row] stringValue]; 
+0

我現在明白了,謝謝。你會認爲這種類型的傳遞將被拾取和生成階段,而不是讓它運行? – Rob4236

+0

您可以在數組中設置對象的類型併發出警告,但是您無法在構建階段傳遞此對象 – AshCenso

相關問題