总和从coreData和显示检索值
我想要添加给定部分中的所有金额标签,并显示在部分标题中的总和。 我的表格视图看起来像这样。总和从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];
}
你需要实现这个方法:- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
它返回给定部分的标题。 因此,您需要确定哪些记录与节号相匹配,并迭代记录以计算总金额。
您可能需要考虑从上述方法中完成此操作,以便每次调用此方法时不计算每个部分的总金额。
为您加载数据,只有做到这一点,您可以立即建立的“预先计算”称号的数组:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [titles objectAtIndex:section];
}
你能编辑你的问题并提供这种方法的代码? – florian 2012-07-27 09:42:48
更新了代码..! – 2012-07-27 09:52:41
的逻辑很简单。我真的不想深入到你的代码,所以这一块会给你你需要一个解释
- (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-27 10:05:57
注意 (我看你不使用fetchResultController,我也用它作为推荐它将帮助你执行此任务和其他任务,并显示核心数据实体的表格视图)。
随着fetchResultController:
您应该添加totalAmount
的NSString您获取实体类(您在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;
}
您应该尝试编写迭代某个节的记录并返回总金额的方法。你的代码不容易理解。我不明白'tableView:cellForRowAtIndexPath:'内循环的目的。 – florian 2012-07-27 11:35:34