0

我的应用程序的一个功能使用核心数据来存储用户的费用作为一个属性。我试图在我的表格视图控制器的第一部分显示所有这些费用的总和。

当总和显示在表格的第 1 部分时,我已经让它完美地工作了。但是,当我使用第 0 节时,它就会中断。我已经调试了该应用程序以找出它在哪里以及为什么会中断。我发现在调用 fetchedResultsController 两次时会出现问题。

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

原因是获取的结果控制器没有任何节,因此所有行都是零节。你将不得不修改你cellForRowAtIndexPath的工作在第 1 部分而不是 0。

你会在你的cellForRowAtIndexPath

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

在第 1 节中,这将简单地返回任何内容。而不是indexPath在上面的行中使用表达式

[NSIndexPath indexPathForRow:indexPath.row inSection:0]

它应该fetchedObjects在第 1 节中显示您的。

至于总和,您可以简单地使用现有的 fetch 生成它:

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