0

私のアプリの関数はコア データを使用して、ユーザーの費用を属性として保存します。テーブル ビュー コントローラーの最初のセクションに、これらすべての費用の合計を表示しようとしています。

合計がテーブルのセクション 1 に表示されると、完全に機能します。ただし、セクション 0 を使用すると壊れてしまいます。アプリをデバッグして、アプリが壊れる場所と理由を調べました。fetchedResultsController を 2 回呼び出すと問題が発生することがわかりました。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        if (section == 1) {
            return 1;       
        }
        if (section == 0){
            return [self.fetchedResultsController.fetchedObjects count]; 
        }
        else return 0;
    }

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {

        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Gastos" inManagedObjectContext:self.managedObjectContext];

    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"nombre" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    _fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![_fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

次のコードは、合計に対して _fetchedResultsController が作成されており、if (_fetchedResultsController != nil) を渡さないため、機能しません。

合計のためだけに別の NSFetchedResultsController を使用する必要がありますか? そうであったとしても、あなたならどうしますか?ありがとう

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (section == 0) {
            return 1;
        } else {
            return [[self.fetchedResultsController fetchedObjects] count];
            //id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        }   
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.section == 0) {
            static NSString *ct = @"CellSum";      
            UITableViewCell *cell = (UITableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:ct];
            [self configureCell:cell atIndexPath:indexPath];
            return cell;
        } else {
            static NSString *ci = @"Cell";      
            UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:ci];
            [self configureCell:cell atIndexPath:indexPath]; 
            return cell;       
        }

    }
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{

        //Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        NSNumber *sum = [self.fetchedResultsController.fetchedObjects
                         valueForKeyPath:@"@sum.precio"];
        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterCurrencyStyle];
        [f setMinimumFractionDigits:2];
        [f setMaximumFractionDigits:2];

        NSString *precio = [f stringFromNumber: sum];

        cell.detailTextLabel.text = [NSString stringWithString:precio];
        cell.textLabel.text = @"Suma Total:";
    } else{
        Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
        cell.textLabel.text = g.categoria.nombre;
        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterCurrencyStyle];
        [f setMinimumFractionDigits:2];
        [f setMaximumFractionDigits:2];

        NSString *precio = [f stringFromNumber: g.precio];

        cell.detailTextLabel.text = [NSString stringWithString:precio];
    }

}
4

1 に答える 1

2

これは、フェッチされた結果コントローラーにセクションがないため、すべての行がセクション 0 になるためです。cellForRowAtIndexPathセクション 0 ではなくセクション 1 で動作するように変更する必要があります。

に次のようなものがありますcellForRowAtIndexPath

NSManagedObject *object = [self.fetchedResultsController 
   objectAtIndexPath:indexPath];

セクション 1 では、何も返されません。indexPath上記の行の代わりに式を使用します

[NSIndexPath indexPathForRow:indexPath.row inSection:0]

fetchedObjectsセクション1にあなたが表示されます。

合計については、既存のフェッチで簡単に生成できます。

NSNumber *sum = [self.fetchedResultsController.fetchedObjects
   valueForKeyPath:@"@sum.nombre"]
于 2012-07-25T20:39:49.943 に答える