NSFetchedResultsController具有节名称 [英] NSFetchedResultsController with section names

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

问题描述

情况:

我有一笔费用跟踪iOS应用程序,我将费用从费用明细视图控制器存储到表视图(带有提取的结果控制器)费用清单以及类别,金额和日期。我在我的实体金钱中有一个日期属性,它是费用或收入的父实体。

I have an expense tracking iOS Application and I am storing expenses from a expense detail view controller into a table view (with fetched results controller) that shows the list of expenses along with the category and amount and date. I do have a date attribute in my entity "Money" which is a parent entity for either an expense or an income.

我的问题:

我想要的是基本分类给定周,一个月或一年的费用/收入,并显示为节标题,例如:(2012年10月1日 - 10月7日)它显示根据该特定周的费用/收入金额和相关的东西。在该视图中提供了两个按钮,如果我按右边的按钮,它将增加一周一个星期(2012年10月1日 - 10月7日现在显示2012年10月8日 - 2012年10月15日),类似地,左按钮将递减一周一周。我在视图中也有两个段控件。我想做的是按下段控制,说每周,如果我按另一个段,说类别 - 我如何根据类别过滤每周的费用/收入?

What I want is to basically categorize my expenses/incomes for a given week, a month, or year and display it as the section header title for example : (Oct 1- Oct 7, 2012) and it shows expenses/incomes amount and related stuff according to that particular week. Two buttons are provided in that view, if I would press the right button, it will increment the week by a week (Oct 1- Oct 7, 2012 now shows Oct8 - Oct 15, 2012) and similarly the left button would decrement the week by a week. I have two segment controls in the view as well. What I want to do is press the segment control which says "weekly" and if I press another segment which says "category" - how would I filter out my expenses/incomes per week based on category ?

我想在表格视图中显示两个部分(一个显示费用日期,另一个显示收入日期格式(Oct1,2012 - Oct7, 2012年))。我将如何实现这一点?我写了一些伪代码,如果有人可以告诉我如何完成上述,这将是巨大的。

I want to display two sections in my table view (one which shows the date for expenses and another to show the date for incomes in format (Oct1,2012 - Oct7, 2012)). How would I achieve this? I have written some pseudo code and if anybody can tell me how would I accomplish the above, that would be great.

编辑 - FETCH控制器

EDIT - FETCH CONTROLLER

- (void)userDidSelectStartDate:(NSDate *)startDate andEndDate:(NSDate *)endDate
{    
    AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext * context = [applicationDelegate managedObjectContext];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Money" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    [NSFetchedResultsController deleteCacheWithName:nil];

    //Here you create the predicate that filters the results to only show the ones with the selected date
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", startDate, endDate];
    [fetchRequest setPredicate:predicate];

    // Set the batch size to a suitable number.
     [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.

    NSSortDescriptor * sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];

    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];

    NSSortDescriptor *sortDescriptor3 = [[NSSortDescriptor alloc] initWithKey:@"cat" ascending:YES];

    NSArray * descriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, sortDescriptor3, nil];

    [fetchRequest setSortDescriptors:descriptors];
    [fetchRequest setIncludesSubentities:YES];
    [fetchRequest setResultType:NSManagedObjectResultType];

    if(_fetchedResultsController)
    {
          [_fetchedResultsController release]; _fetchedResultsController = nil;

    }
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"type" cacheName:nil];

    _fetchedResultsController.delegate = self;

    NSError *anyError = nil;

    if(![_fetchedResultsController performFetch:&anyError])
    {
        NSLog(@"error fetching:%@", anyError);
    }

    [sortDescriptor1 release];
    [sortDescriptor2 release];
    [sortDescriptor3 release];

    [fetchRequest release];

     //Finally you tell the tableView to reload it's data, it will then ask your NEW FRC for the new data
    [self.dashBoardTblView reloadData];

}

EDIT - 获取控制器委托方法

EDIT - Fetch Controller Delegate Methods

- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller
{
     [self.dashBoardTblView beginUpdates];

}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
       atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

   switch(type) {
    case NSFetchedResultsChangeInsert:
        [self.dashBoardTblView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                      withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [self.dashBoardTblView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                      withRowAnimation:UITableViewRowAnimationFade];
        break;
   }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{

    switch(type) {
    case NSFetchedResultsChangeInsert:

        [self.dashBoardTblView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:

        [self.dashBoardTblView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeMove:

        [self.dashBoardTblView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        [self.dashBoardTblView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                         withRowAnimation:UITableViewRowAnimationFade];


        break;

    case NSFetchedResultsChangeUpdate:
    {
        [self.dashBoardTblView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;
    }
  }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.dashBoardTblView endUpdates];
} 


推荐答案

/收入对象分成不同的部分,您需要向 Money 实体添加类型属性,例如属性 0 用于支出, 1 用于收入

To group the "Expense"/"Income" objects into different sections, you need to add a type attribute to the Money entity, for example a Integer attribute that is 0 for expenses and 1 for incomes.

要按类型(费用/收入)对表视图节进行排序,请先使用键入 sort descriptor:

To sort the table view sections by type (expense/income), use type as first sort descriptor:

NSSortDescriptor *s1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];

要按日期对每个部分中的条目排序,请使用 date 作为第二个排序描述符:

To sort the entries within each section by date, use date as second sort descriptor:

NSSortDescriptor *s2 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];

NSArray *descriptors = [NSArray arrayWithObjects:s1, s2, nil];
[fetchRequest setSortDescriptors:descriptors];

最后,要根据类别将表视图分成几个部分,请使用 / code> as sectionNameKeyPath

Finally, to group the table view into sections by category, use type as sectionNameKeyPath:

NSFetchedResultsController *aFetchedResultsController =
   [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                       managedObjectContext:context
                                         sectionNameKeyPath:type
                                                  cacheName:nil];

这应该给出2节,第一节包含所有费用,第二节包含所有收入。

This should give 2 sections, with all expenses in the first section and all incomes in the second section.

现在对于节头你必须实现 tableView:viewForHeaderInSection:(我希望我得到这个权利) :

Now for the section headers you have to implement tableView:viewForHeaderInSection: (I hope that I get this right):

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    Money *money = [[sectionInfo objects] objectAtIndex:0]; // The first object in this section
    NSNumber *type = money.type;
    if (type.intValue == 0) {
        // Create and return header view for "expense" section.

    } else {
        // Create and return header view for "incomes" section.

    }
}

这篇关于NSFetchedResultsController具有节名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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