• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 在 UITableView 中创建运行总和

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:10:09 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 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;
}



Best Answer-推荐答案


基本上你想要做的是:每次调用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap