2012-07-27 61 views
4

我想要添加給定部分中的所有金額標籤,並顯示在部分標題中的總和。 我的表格視圖看起來像這樣。總和從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]; 



} 
+0

您應該嘗試編寫迭代某個節的記錄並返回總金額的方法。你的代碼不容易理解。我不明白'tableView:cellForRowAtIndexPath:'內循環的目的。 – florian 2012-07-27 11:35:34

回答

0

你需要實現這個方法:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
它返回給定部分的標題。 因此,您需要確定哪些記錄與節號相匹配,並迭代記錄以計算總金額。
您可能需要考慮從上述方法中完成此操作,以便每次調用此方法時不計算每個部分的總金額。
爲您加載數據,只有做到這一點,您可以立即建立的「預先計算」稱號的數組:

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

你能編輯你的問題並提供這種方法的代碼? – florian 2012-07-27 09:42:48

+0

更新了代碼..! – 2012-07-27 09:52:41

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

我沒有使用任何字典..我已更新我的代碼..請檢查 – 2012-07-27 10:05:57

0

注意 (我看你不使用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; 
}