2

I have a UITableView populated (previously saved via a button in navbar), with transactions. Each row has five UILabel: date, description, person, value (deposits and withdraws) balance. The table is sort by date. How can I obtain the daily balance? The balance is the sum (+ and -) from the first input up to the current row.

I have the following code (JournalDetail.m), but I only get the same amount as value, instead of a cumulative amount (balance)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
GxPolizasL *cell = (GxPolizasL *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"GxPolizasL" owner:self options:nil];
cell = gxPolizasL;
self.gxPolizasL = nil;
GMDiario *gmDiarioy = (GMDiario *)[self.fetchedResultsController objectAtIndexPath:indexPath];
//Date value
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yy"];
cell.fechao.text = [df stringFromDate:gmDiarioy.pzFecha];
[df release];
//Description value
cell.referencia.text=gmDiarioy.pzAlias;
//Person value
cell.item.text = gmDiarioy.persona.prAlias;
//Transaction value
NSNumberFormatter* vf = [[NSNumberFormatter alloc] init];    
[vf setNumberStyle:NSNumberFormatterCurrencyStyle];
cell.valuem.text= [vf stringFromNumber: gmDiarioy.pzMont01];
[vf release];
//This is where the balance value is intended to be created
NSDecimalNumber *saldoAc = [NSDecimalNumber decimalNumberWithString:@"0.0"];
NSDecimalNumber *objectExpenseNumber = [gmDiarioy valueForKeyPath:@"pzMont01"];
saldoAc = [saldoAc decimalNumberByAdding:objectExpenseNumber];
NSLog(@"saldoAc: %@",saldoAc);
NSNumberFormatter* af = [[NSNumberFormatter alloc] init];
[af setNumberStyle:NSNumberFormatterCurrencyStyle];
cell.valuea.text= [af stringFromNumber: saldoAc];
[af release];
}
return cell;}

Could you help me to figure out which is the correct way to implement balance value? Thanks in advance. Your help is greatly appreciated.

This is the code for self.fetchedResultsController

- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];

//Diario is an entity that keeps all transactions
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Diario" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];    

//This is to select only the journals for the current account (value passed at didSelectRowAtIndexPath from the previous UItableView
NSString *filtro=gmCuenta.cnAlias;  
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cuenta.cnAlias like %@", filtro];
NSLog(@"NSPredicate1 %@", gmDiario.cuenta);
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"pzFecha" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil] autorelease];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();}
return __fetchedResultsController;
}
Memo
  • 39
  • 7

1 Answers1

4

Basically what you want to do is this: Each time cellForRowAtIndexPath: gets called you want to loop through your data source array and sum all the values starting at the beginning of the array and ending with the array item equal to the row # (i.e. indexPath.row) of the cell in question. That will give you a running total (balance).

NSDecimalNumber *saldoAc = [NSDecimalNumber decimalNumberWithString:@"0.0"];
for (int i=0; i <= indexPath.row; i++) {
    NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:i];
    GMDiario *tempObj = (GMDiario *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    NSDecimalNumber *objectExpenseNumber = [tempObj valueForKeyPath:@"pzMont01"];
    saldoAc = [saldoAc decimalNumberByAdding:objectExpenseNumber];
}
NSNumberFormatter* af = [[NSNumberFormatter alloc] init];
[af setNumberStyle:NSNumberFormatterCurrencyStyle];
cell.valuea.text= [af stringFromNumber: saldoAc];
[af release];

One other comment - you should be setting the values of your cell's subviews after you exit the if (cell == nil) block. Otherwise you will run into trouble when your tableView starts scrolling.

Rayfleck
  • 12,116
  • 8
  • 48
  • 74
jonkroll
  • 15,682
  • 4
  • 50
  • 43
  • jonkroll, thanks for your time. I've input your suggestion, but I received a warning: incompatible integer to pointer conversion sending 'int' to parameter of type NSIndexPath. am I missing something? – Memo Jan 28 '12 at 01:59
  • Oops, that's right. Obviously `objectAtIndexPath:` requires an NSIndexPath object as an argument. I've corrected the code above. – jonkroll Jan 28 '12 at 02:11
  • for some reason it crash at line: GMDiario *tempObj = (GMDiario *)[self.fetchedResultsController objectAtIndexPath:indexPath]; how can I figure out the problem. – Memo Jan 28 '12 at 02:26
  • I made some debugging and it says:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 2147483647 in section at index 0' *** First throw call stack: (0x1a00052 0x1b91d0a 0x115b9d9 0x190ec 0x23ee0f 0x23f589 0x22adfd 0x239851 0x1e4301 0x1a01e72 0x23aa92d 0x23b4827 0x233afa7 0x233cea6 0x233c580 0x19d49ce 0x196b670 0x19374f6 0x1936db4 0x1936ccb 0x18e9879 0x18e993e 0x1a5a9b 0x2032 0x1fa5) terminate called throwing an exceptionSingle stepping until exit from function __kill,which has no line number information – Memo Jan 28 '12 at 02:34
  • I guess is "no object at index.." – Memo Jan 28 '12 at 02:35
  • My table is grouped. I only have one section and many rows as journals – Memo Jan 28 '12 at 02:44
  • well, it's hard to say without knowing your data structure better. What type of object is `self.fetchedResultsController`? Is it possible there are more rows in your table than there are transactions in that object? – jonkroll Jan 28 '12 at 02:53
  • I've included the code for fetchedResultsController. The fetch is done over the Diario entity (journals) – Memo Jan 28 '12 at 03:03
  • No. Before this adjustment my table only shows the transactions that belongs to a given account. I made a reset (to erase all), ran the app, and still crashes. – Memo Jan 28 '12 at 03:06
  • Now its working. I made a little adjustment to your example (I'm using grouped table, therefore, section was missing; however your help makes the difference. Answer vote +1. I update your code. Thanks for your support. – Memo Jan 28 '12 at 04:36