cellForRowAtIndexPath的小计

时间:2012-01-27 18:14:56

标签: iphone ios uitableview core-data

我有两个实体,期刊和帐户。这种关系是一对多的:Journal只有一个帐户。帐户有很多期刊。我想根据其期刊显示每个帐户的总数,但我不知道如何。目前,我为每个帐户获得的总数是相同的:

在我的UITableView(Accounts.m)中,我有:

- (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:@"journals.@sum.pzAmount01"];
            balanceC = [balanceC decimalNumberByAdding:objectExpenseNumber];
        }
        NSNumberFormatter* bf = [[NSNumberFormatter alloc] init];    
        [bf setNumberStyle:NSNumberFormatterCurrencyStyle];
        cell.cnSaldo.text = [bf stringFromNumber: balanceC];
        [bf release];

即使我只有两个帐户,以及与每个帐户相关的多个日记帐,我也会为每个帐户获得相同的金额。如何为每个帐户显示正确的余额?请你帮助我好吗?。提前致谢。

1 个答案:

答案 0 :(得分:1)

确定。似乎您在if(cell == nil)块中设置了您的单元格标签。这是不正确的。请注意,在tableView中,单元格被回收,并且当创建足够数量的单元格时,不再调用该块,因为旧单元格在回收队列中可用。好。这里正确的方式如下。 (检查语法,我在这里写了)。

    - (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:@"journals.@sum.pzAmount01"];
            balanceC = [balanceC decimalNumberByAdding:objectExpenseNumber];}
    NSNumberFormatter* bf = [[NSNumberFormatter alloc] init];    
        [bf setNumberStyle:NSNumberFormatterCurrencyStyle];
        cell.cnSaldo.text= [bf stringFromNumber: balanceC];
        [bf release];
}

return cell;

}