我已经填充了UITableView(以前通过导航栏中的按钮保存),包含事务。每行有五个UILabel:日期,描述,人,价值(存款和取款)余额。该表按日期排序。我怎样才能获得每日余额?余额是从第一个输入到当前行的总和(+和 - )。
我有以下代码(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;
}
答案 0 :(得分:4)
基本上你想要做的是:每次调用cellForRowAtIndexPath:
时,你想循环遍历你的数据源数组并总结从数组开头开始并以数组项等于的数字结束的所有值相关单元格的行#(即indexPath.row
)。这将给你一个总计(余额)。
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];
另一条评论 - 您应该在退出if (cell == nil)
区块后设置单元格子视图的值。否则,当tableView开始滚动时,您将遇到麻烦。