Summarize the values ​​obtained from coreData and Display in the section

I want to add all quantity labels in this section and display the total quantity in the section header. my view of the table is as follows.

Section Title - 07/12/2012

categoryName Amount Groceries 100 Social 100 

Section Title - 04/10/2012

 categoryName Amount Gas 500 Social 600 

now I want to display a total of 200 in the title of the 1st section. and 1100 in the next title

 -(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
source share
3 answers

You need to implement this method:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
It returns the title for this section. Therefore, you need to find out which records correspond to the section number and iterate over the records to calculate the total amount.
You may want to do this from the above method, so as not to calculate the total amount for each section each time this method is called.
You can create an array of “pre-computed” names as soon as you upload your data, and just do it:

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

The logic is pretty simple. I really don't want to go deep into your code, so this snippet will give you the explanation you need

 - (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]; } 
0
source

Note (I see that you are not using fetchResultController, and I recommend using it, as it will help you with this task and other tasks with a table view displaying the main data objects).

With fetchResultController:

You must add the totalAmount NSString to the object of your choice (the object you get in the fetchResultController class).

Then add the add operation to the class. inside, create your calculation to get the total amount as a string.

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

and then in the resulting selection controller pass totalAmount as the path to the key of your section, this will return the string totalAmount for each section:

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

Then edit the section header methods so that the table view works correctly:

 - (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]; 

finally, set the quantity text for each section from the sample result controller:

  - (NSString *)controller:(NSFetchedResultsController *)controller sectionIndexTitleForSectionName:(NSString *)sectionName { return sectionName; } 
0
source

All Articles