我是否需要第二个NSFetchedResultsController [英] Do I need a second NSFetchedResultsController

查看:55
本文介绍了我是否需要第二个NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A function of my app uses core data to store expenses of user as an attribute. I'm trying to display the sum of all these expenses on the first section of my table view controller.

当总和显示在我表的第1部分。但是,当我使用第0部分时,它就坏了。我已经调试了该应用程序,以找出中断的位置和原因。我发现调用fetchedResultsController两次时出现问题。

I've got it working perfectly when the sum is displayed at the section 1 of my table. However when I use the section 0 it just breaks. I've debugged the app to find out where and why it breaks. I figured out that the problem comes up when calling fetchedResultsController twice.

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

The following code won't work because the _fetchedResultsController has been created for the sum, and will no pass the if (_fetchedResultsController != nil).

我是否需要仅使用另一个NSFetchedResultsController作为总和?不管是哪种情况,您将如何做?谢谢

Do I need to use another NSFetchedResultsController just for the sum? Whether it's the case, how would you do it? Thanks

    - (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];
    }

}


推荐答案

原因是提取的结果控制器没有任何节,因此所有行的节均为零。您将必须修改您的 cellForRowAtIndexPath 以在第1部分而不是0中工作。

The reason is that the fetched results controller does not have any sections, so all rows are of section zero. You will have to modify your cellForRowAtIndexPath to work in section 1 rather than 0.

您将拥有类似的东西在您的 cellForRowAtIndexPath 中:

You will have something like this in your cellForRowAtIndexPath:

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

在第1节中,这将仅返回任何内容。代替上一行中的 indexPath ,使用表达式

In section 1 this will simply return nothing. Instead of indexPath in the above line use the expression

[NSIndexPath indexPathForRow:indexPath.row inSection:0]

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

and it should display your fetchedObjects in section 1.

对于总和,您可以简单地通过现有的获取生成它:

As for the sum, you can simply generate it with the existing fetch:

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

这篇关于我是否需要第二个NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆