4

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

部分标题 - 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];



}
4

3 回答 3

0

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
{  
    return [titles objectAtIndex:section];  
}
于 2012-07-27T09:37:28.027 回答
0

逻辑很简单。我真的不想深入你的代码,所以这个块会给你一个你需要的解释

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  NSArray *sectionArray = [mySectionsDictionary objectForKey:[NSString stringWithFormat:@"%d", section]];
  int total = 0;
  for (MyCustomCoreDataObject *object in sectionArray) {
    total += object.amount.intValue;
  }
  return [NSString stringWithFormat:@"%d", total];
}
于 2012-07-27T09:38:05.113 回答
0

注意 (我看到您没有使用 fetchResultController,我建议您使用它,因为它可以帮助您完成此任务和其他使用显示核心数据实体的表格视图的任务)。

使用 fetchResultController:

您应该将totalAmountNSString 添加到您获取的实体(您在 fetchResultController 中获取的实体)类。

然后将添加操作添加到类中。在里面创建你的计算,这样你就会得到一个字符串的总量。

-(NSString*)totalAmount{
   NSInteger amount = 0;
   //do your calculation here
   return [NSString stringWithFormat:@"amount = %i",amount];
}

然后在获取结果控制器中传递totalAmount您的部分名称键路径,这将返回totalAmount每个部分的字符串:

NSFetchedResultsController *fetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:request
                                    managedObjectContext:managedObjectContext
                                      sectionNameKeyPath:@"totalAmount" cacheName:nil];

然后编辑您的部分标题方法,以便表格视图正确运行:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
     return ([[fetchedResultsController sections] count]);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
      id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];

最后,从获取结果控制器中为每个部分设置金额文本:

 - (NSString *)controller:(NSFetchedResultsController *)controller sectionIndexTitleForSectionName:(NSString *)sectionName {
      return sectionName;
 }
于 2012-07-27T09:50:47.913 回答