我想要添加給定部分中的所有金額標籤,並顯示在部分標題中的總和。 我的表格視圖看起來像這樣。總和從coreData和顯示檢索值
段頭 - 12/07/2012
categoryName Amount
Groceries 100
Social 100
段頭 - 10/04/2012
categoryName Amount
Gas 500
Social 600
現在我想在第一部分1100 header.and顯示總200在接下來的頭
-(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];
}
您應該嘗試編寫迭代某個節的記錄並返回總金額的方法。你的代碼不容易理解。我不明白'tableView:cellForRowAtIndexPath:'內循環的目的。 – florian 2012-07-27 11:35:34