我有一个 UITableView 填充(以前通过导航栏中的按钮保存),带有事务。每行有五个 UILabel:日期、描述、人员、值(value)(存款和取款)余额。该表按日期排序。如何获取每日余额?余额是从第一个输入到当前行的总和(+ 和 -)。
我有以下代码(JournalDetail.m),但我只得到与值(value)相同的金额,而不是累积金额(余额)
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 decimalNumberByAddingbjectExpenseNumber];
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;
}
基本上你想要做的是:每次调用 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 decimalNumberByAddingbjectExpenseNumber];
}
NSNumberFormatter* af = [[NSNumberFormatter alloc] init];
[af setNumberStyle:NSNumberFormatterCurrencyStyle];
cell.valuea.text= [af stringFromNumber: saldoAc];
[af release];
另一条评论 - 您应该在退出 if (cell == nil)
block 后设置单元格 subview 的值。否则当你的 tableView 开始滚动时你会遇到麻烦。
关于iphone - 在 UITableView 中创建运行总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9042126/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |