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 {
  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 を使用:

totalAmount取得したエンティティ (fetchResultController で取得したエンティティ) クラスに NSString を追加する必要があります。

次に、追加操作をクラスに追加します。内部で計算を作成して、合計金額を文字列として取得します。

-(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 に答える
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 に答える