我需要第二个 NSFetchedResultsController

Posted

技术标签:

【中文标题】我需要第二个 NSFetchedResultsController【英文标题】:Do I need a second NSFetchedResultsController 【发布时间】:2012-07-25 14:37:00 【问题描述】:

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

当总和显示在表格的第 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 已为 sum 创建,并且不会通过 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];
    


【问题讨论】:

你发了两次numberOfRowsInSection,没必要。您没有发布索引路径为 0,0 的 cellForRowAtIndexPath 的相关部分。 【参考方案1】:

原因是获取的结果控制器没有任何节,因此所有行都是零节。您必须修改您的 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"]

【讨论】:

Mundi,你的回答成功了!太感谢了。我通过添加解决方案更新了我上面的问题 您的方法看起来很适合显示内容。但是,如果您深入研究,则会遇到一些问题。如果进行插入,则该表将遭受奇怪的行为: case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:newIndexPath.row inSection:1]] withRowAnimation:UITableViewRowAnimationFade]; TV 部分 1 将显示前 5 个获取的结果。但是,如果我重新启动应用程序,则数据会正确显示。只需少量编码即可删除和更新,有什么建议吗? 没有奇怪的行为。您必须实现 NSFetchedResultsController 委托方法并相应地修改它们(即插入/删除具有正确 indexPaths 的行)。 请在 mu 以前的评论中更新代码。无论如何,我的答案接近解决方案。干杯

以上是关于我需要第二个 NSFetchedResultsController的主要内容,如果未能解决你的问题,请参考以下文章

从文本文件中提取一行中的第二个单词

$in 需要一个数组作为第二个参数

第一个或第二个输入表单需要

在第二个视图中获取 WebView AbsoluteString

需要帮助找出为啥我的应用程序在单击按钮后没有离开主屏幕(这应该带我进入第二个活动屏幕)

第一个下拉菜单更改第二个下拉菜单相关项目显示