在 UITableView 中创建运行总和
Posted
技术标签:
【中文标题】在 UITableView 中创建运行总和【英文标题】:Create a running sum in a UITableView 【发布时间】:2012-01-28 01:16:03 【问题描述】:我有一个 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;
【问题讨论】:
【参考方案1】:基本上您想要做的是:每次调用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 开始滚动时你会遇到麻烦。
【讨论】:
jonkroll,感谢您的宝贵时间。我已经输入了你的建议,但我收到了一个警告:不兼容的整数到指针转换将“int”发送到 NSIndexPath 类型的参数。我错过了什么吗? 糟糕,没错。显然objectAtIndexPath:
需要一个 NSIndexPath 对象作为参数。我已经更正了上面的代码。
由于某种原因它在以下行崩溃: GMDiario *tempObj = (GMDiario *)[self.fetchedResultsController objectAtIndexPath:indexPath];我怎样才能找出问题所在。
我做了一些调试,它说:*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“索引 0 部分的索引 2147483647 处没有对象”*** 首先抛出调用堆栈:(0x1a00052 0x1b91d0a 0x115b9d9 0x190ec 0x23ee0f 0x23f589 0x22adfd 0x239851 0x1e4301 0x1a01e72 0x23aa92d 0x23b4827 0x233afa7 0x233cea6 0x233c580 0x19d49ce 0x196b670 0x19374f6 0x1936db4 0x1936ccb 0x18e9879 0x18e993e 0x1a5a9b 0x2032 0x1fa5)终止称为抛出exceptionSingle步进,直到从功能__kill,它没有行号信息出口跨度>
我猜是“索引处没有对象..”以上是关于在 UITableView 中创建运行总和的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UITableview 中创建具有 n 行数的 UITextField