2

トランザクションを使用して、UITableView を入力しました (以前はナビゲーション バーのボタンを使用して保存しました)。各行には 5 つの UILabel があります: 日付、説明、人、値 (入出金) の残高です。表は日付順です。1日の残高はどのように取得できますか? 残高は、最初の入力から現在の行までの合計 (+ と -) です。

次のコード (JournalDetail.m) がありますが、累積額 (残高) ではなく、値と同じ額しか得られません。

- (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;}

バランス値を実装する正しい方法を教えてください。前もって感謝します。よろしくお願いいたします。

これは 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;
}
4

1 に答える 1

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.

于 2012-01-28T01:48:06.037 に答える