gpt4 book ai didi

objective-c - 对从 coreData 检索到的值求和并在部分中显示

转载 作者:可可西里 更新时间:2023-11-01 06:16:36 24 4
gpt4 key购买 nike

我想在给定部分中添加所有金额标签,并在部分标题中显示总数。我的表格 View 看起来像这样。

章节标题 - 12/07/2012

categoryName  Amount
Groceries 100
Social 100

章节标题 - 2012 年 10 月 4 日

categoryName  Amount
Gas 500
Social 600

现在我想在第一节标题中显示总数 200,在下一个标题中显示 1100

-(IBAction)bydate:(id)sender
{
[self.expenseArray removeAllObjects];
[self.expenseArrayCount removeAllObjects];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

for(NSManagedObject *records in self.listOfExpenses){
NSString *compareDates = [dateFormatter stringFromDate:[records valueForKey:@"date"]];
BOOL isAvail = NO;
for (int i = 0; i<[self.expenseArray count]; i++){
if([compareDates isEqualToString:[self.expenseArray objectAtIndex:i]])
isAvail = YES;
}
if(!isAvail)
[self.expenseArray addObject:compareDates];
}
int count = 0;
for (int i = 0 ; i < [self.expenseArray count] ; i ++){
NSString *compareDates = [self.expenseArray objectAtIndex:i];
for(NSManagedObject *info in self.listOfExpenses){
if([compareDates isEqualToString:[dateFormatter stringFromDate:[info valueForKey:@"date"]]])
count++;
}
[self.expenseArrayCount addObject:[NSNumber numberWithInt:count]];
count = 0;
}
[self.byDateTab reloadData];
[dateFormatter release];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//not posting un necessary code.

else if (tableView == self.byDateTab)
{
for(int i = 0; i < [self.expenseArrayCount count];i++)
{
if(indexPath.section == 0)
{
NSManagedObject *records = nil;
records = [self.listOfExpenses objectAtIndex:indexPath.row];
self.firstLabel.text = [records valueForKey:@"category"];
self.secondLabel.text = [records valueForKey:@"details"];
NSString *amountString = [NSString stringWithFormat:@"%@",[records valueForKey:@"amount"]];
self.thirdLabel.text = amountString;
}
else if (indexPath.section == i)
{
int rowCount = 0;
for(int j=0; j<indexPath.section; j++)
{
rowCount = rowCount + [[self.expenseArrayCount objectAtIndex:j]intValue];
}
NSManagedObject *records = nil;
records = [self.listOfExpenses objectAtIndex:(indexPath.row + rowCount) ];
self.firstLabel.text = [records valueForKey:@"category"];
self.secondLabel.text = [records valueForKey:@"details"];
NSString *amountString = [NSString stringWithFormat:@"%@",[records valueForKey:@"amount"]];
self.thirdLabel.text = amountString;

}
}
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = [[[NSString alloc]init]autorelease];
if(tableView == self.byDateTab)
title = [self.expenseArray objectAtIndex:section];
return title;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc]init] autorelease];
if(tableView == self.byDateTab){
headerView.frame = CGRectMake(310, 0, tableView.bounds.size.width, 30);
[headerView setBackgroundColor:[UIColor colorWithRed:149.0/255 green:163.0/255 blue:173.0/255 alpha:1]];
UILabel *leftLabel = [[[UILabel alloc]initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width-10, 18)]autorelease];
leftLabel.text = [self tableView:tableView titleForHeaderInSection:section];
leftLabel.textColor = [UIColor whiteColor];
leftLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:17.0];
leftLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:leftLabel];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
NSString *a = [formatter currencySymbol];

UILabel *rightLabel = [[[UILabel alloc]initWithFrame:CGRectMake(250, 3, tableView.bounds.size.width-10, 18)]autorelease];
rightLabel.text = [NSString stringWithFormat:@"(%@)",a];
rightLabel.textColor = [UIColor whiteColor];
rightLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:17.0];
rightLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:rightLabel];



}

最佳答案

你需要实现这个方法:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
它返回给定部分的标题。因此,您需要找出哪些记录与节号匹配,并遍历记录以计算总数。
您可能需要考虑在上述方法之外执行此操作,以便在每次调用此方法时不计算每个部分的总金额。
您可以在加载数据后立即创建一组“预计算”标题,并且只执行以下操作:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
{
return [titles objectAtIndex:section];
}

关于objective-c - 对从 coreData 检索到的值求和并在部分中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11684972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com