我想在给定部分中添加所有金额标签并在部分标题中显示总数。我的表格视图看起来像这样。
部分标题 - 2012 年 12 月 7 日
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];
}