我需要第二个NSFetchedResultsController吗?

时间:2012-07-25 14:37:01

标签: uitableview ios5 core-data

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

当我在桌子的第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;
}

以下代码无法正常工作,因为已经为sum创建了_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];
    }

}

1 个答案:

答案 0 :(得分:2)

原因是获取的结果控制器没有任何部分,因此所有行都是零部分。您必须修改cellForRowAtIndexPath才能在第1部分而不是0部分中工作。

您的cellForRowAtIndexPath

会有类似的内容
NSManagedObject *object = [self.fetchedResultsController 
   objectAtIndexPath:indexPath];

在第1部分中,这将不会返回任何内容。而不是上面一行中的indexPath使用表达式

[NSIndexPath indexPathForRow:indexPath.row inSection:0]

并且应该在第1部分中显示您的fetchedObjects

至于总和,您只需使用现有的fetch生成它:

NSNumber *sum = [self.fetchedResultsController.fetchedObjects
   valueForKeyPath:@"@sum.nombre"]