Memo
Memo

Reputation: 39

Subtotal at cellForRowAtIndexPath

I have two entities, Journal and Accounts. The relationship is one-to many: Journal has only one Account. Account has many journals. I want to show the total for each Account based on its Journals, but I don't know how. Currently the total I am getting for each account is the same:

In my UITableView (Accounts.m) I have:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
    static NSString *CellIdentifier = @"Cell";
    XbAccountsN *cell = (XbAccountsN *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"XbAccountsN" owner:self options:nil];
        cell = xbAccountsN;
        self.xbAccountsN = nil;
        GMAccount *awCuentasy = (GMAccount *)[self.fetchedResultsController objectAtIndexPath:indexPath];
        cell.cnAlias.text = awCuentasy.cnAlias;
        NSDecimalNumber *saldoAc = [NSDecimalNumber decimalNumberWithString:@"0.0"];
        for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects]) {
            NSLog( @"Looping");
            NSDecimalNumber *objectExpenseNumber = [object valueForKeyPath:@"[email protected]"];
            balanceC = [balanceC decimalNumberByAdding:objectExpenseNumber];
        }
        NSNumberFormatter* bf = [[NSNumberFormatter alloc] init];    
        [bf setNumberStyle:NSNumberFormatterCurrencyStyle];
        cell.cnSaldo.text = [bf stringFromNumber: balanceC];
        [bf release];

I get the same amount for each account, even if I have just two accounts, and several journals related to each account. How can I present the correct balance for each account? Could you please help me?. Thanks in advance.

Upvotes: 0

Views: 369

Answers (1)

Vincent Zgueb
Vincent Zgueb

Reputation: 1491

Ok. It seems you are setting your cell label in the if (cell == nil) block. It is not correct. Notice that in a tableView the cells are recycled and when a sufficient amount of cells are created the block is no longer called because old cells are available in the recycling queue. Ok. Here the right way below. (check the syntax, I wrote it here).

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

    XbAccountsN *cell = (XbAccountsN *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    if (cell == nil) {
        // Init new cell from nib archive (assuming the cell is the top most object in your xib)
        cell = (UITableviewCell*)[[[NSBundle mainBundle] loadNibNamed:@"XbAccountsN" owner: nil options:nil] objectAtIndex: 0];
    }

// Calculate your sum for the given awCuentasy
    GMAccount *awCuentasy = (GMAccount *)[self.fetchedResultsController objectAtIndexPath:indexPath];

    cell.cnAlias.text= awCuentasy.cnAlias;
    NSDecimalNumber *saldoAc = [NSDecimalNumber decimalNumberWithString:@"0.0"];
    for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects]) {
            NSLog( @"Looping");
            NSDecimalNumber *objectExpenseNumber = [object valueForKeyPath:@"[email protected]"];
            balanceC = [balanceC decimalNumberByAdding:objectExpenseNumber];}
    NSNumberFormatter* bf = [[NSNumberFormatter alloc] init];    
        [bf setNumberStyle:NSNumberFormatterCurrencyStyle];
        cell.cnSaldo.text= [bf stringFromNumber: balanceC];
        [bf release];
}

return cell;

}

Upvotes: 1

Related Questions