I am getting an error when on a simple TableViewController Project as it looks like I am trying to force an NSDecimalNumber
object into the text of a label called CheckValue.text
as part of the cellForRowAtIndexPath
method.
And truth be told, I am trying to do this as I need to value of the NSDecimalNumber
to be displayed on the TableCell. Can anyone help as I feel like I am close to a bullet here?
Here is the code from the 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
You can format a string with the NSDecimalNumber value
cell.CheckValue.text = [NSString stringWithFormat:@"%@",_CheckValue[row]]