0

我在核心数据中有一个名为“费用”的实体,其属性为“日期、类别、金额…………”。如何根据 Date 属性中的“Year-Month”列出索引列表中的所有费用?

从苹果的文档和教程中,我知道如何制作索引列表表格视图。但它需要提供“数组数组”。这是所有部分的一个数组,一个特定部分中的对象的一个​​子数组。现在我的数据结构不是这样的。

根据我的想法,我能做的是:

  1. 先取出所有的费用,然后提取所有“年月”的情况,不重复,放入一个数组,用于所有的部分
  2. 然后我根据每个部分获取所有费用

但我认为这是一项繁重的工作,我可以更方便地实现这一点吗?或者我应该改变我的数据结构?谢谢你们 :)

4

2 回答 2

1

您可以创建一个请求以仅返回给定范围内的费用:

//Not tested
NSDateComponents* comps = [[NSDateComponents alloc] init];
NSCalendar* cal = [NSCalendar currentCalendar];
NSInteger year,month;//set these as you like
[comps setYear:year];
[comps setMonth:month];
NSDate* start = [cal dateFromComponents:comps];
month += 1;
month = (month <= 12 ? : 1);
[comps setMonth:month];
NSDate* end = [cal dateFromComponents:comps];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"date > %@ AND date < %@",start,end];
NSFetchRequest* request = [[NSFetchRequest alloc] initWithEntityName:@"Expense"];
[request setPredicate:predicate];

然后使用 aNSFetchedResultsController填充您的表格视图

于 2013-05-13T23:54:06.653 回答
0

这是一个非常笼统的问题,但基本思想是使用 NSFetchedResultsController 显示数据并使用 NSPredicate 进行过滤。

我还强烈建议您查看 Sensible TableView 等框架,因为它可以帮助您自动显示和过滤开箱即用。

于 2013-05-13T23:56:02.747 回答